0% found this document useful (0 votes)
77 views25 pages

Maven & Junit Introduction

This document provides an introduction to Maven and JUnit. It discusses the Maven build lifecycle, pom.xml file, directory structure, and archetypes. It also covers what JUnit is used for, how to write JUnit tests, and how Maven integrates with JUnit via the Surefire plugin to execute unit tests during the build lifecycle.

Uploaded by

Amine Besrour
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views25 pages

Maven & Junit Introduction

This document provides an introduction to Maven and JUnit. It discusses the Maven build lifecycle, pom.xml file, directory structure, and archetypes. It also covers what JUnit is used for, how to write JUnit tests, and how Maven integrates with JUnit via the Surefire plugin to execute unit tests during the build lifecycle.

Uploaded by

Amine Besrour
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Maven & JUnit introduction

Agenda

● Introduction
● Build lifecycle
● pom.xml
● Directory layout
● Archetypes
● JUnit
Introduction

● Introduction
● Build lifecycle
● pom.xml
● Directory layout
● Archetypes
● JUnit
Introduction

How to compile java project

● Use command line and javac


● Use your IDE
● Use make
● Use ANT (Another Neat Tool)
● Use Maven
● Use Gradle
Introduction

Why maven?

● De facto standard
● Able to compile, test, pack, distribute source code
● Robust dependency management
● Extensible via plugins
● Large community (there are a lot of different plugins and solutions
for maven)
Build lifecycle

● Introduction
● Build lifecycle
● pom.xml
● Directory layout
● Archetypes
● JUnit
Build lifecycle

● Maven relies on build lifecycle to define the process of building and


distributing artifacts (jar, war, ear, etc)
● There are 3 build-in lifecycles
○ Default (handles project building and deployment)
○ Clean (handles project cleaning)
○ Site (handles project site documentation generation)
Lifecycle phases

https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference

Validate Compile Test Package Integration test Verify Install

Deploy
Lifecycle phases

● You can call specific phase on the lifecycle (ex: mvn test)
● It will execute not only that build phase, but also every build phase
prior to the called build phase.

Validate Compile Test


pom.xml

● Introduction
● Build lifecycle
● pom.xml
● Directory layout
● Archetypes
● JUnit
pom.xml

● POM stands for Project Object Model. It is an XML file that always
resides in the base directory as pom.xml

● POM contains information about project, dependencies, and various


configuration details used by Maven to build the project(s).
pom.xml sample

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.epam</groupId>
<artifactId>unit-test-sample</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Maven jUnit sample</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.epam</groupId>
<artifactId>unit-test-sample</artifactId>
<version>1.0-SNAPSHOT</version>

<packaging>jar</packaging>

<name>Maven jUnit sample</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Directory Layout

● Introduction
● Build lifecycle
● pom.xml
● Directory layout
● Archetypes
● JUnit
Default directory layout

.
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └─ epam
│ │ └── unittest
│ │ └─ Main.java
│ └── resources
└── test
├── java
│ └── com
│ └─ epam
│ └─ unittest
│ └─ MainTest.java
└── resources
Default directory layout

.
├── pom.xml This part is common for each java maven project
└── src
├── main
│ ├── java
│ │ └── com
│ │ └─ epam
│ │ └── unittest
│ │ └─ Main.java
│ └── resources
└── test
├── java
│ └── com
│ └─ epam
│ └─ unittest
│ └─ MainTest.java
└── resources
Archetypes

● Introduction
● Build lifecycle
● pom.xml
● Directory layout
● Archetypes
● JUnit
Maven archetype

What is archetype?

● archetype is maven plugin whose task is to create a project structure


as per its template
● It helps user to to quickly start a new java project using command
mvn archetype:generate

Quickstart archetype generates “Hello world” project with unit test

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app


-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
JUnit

● Introduction
● Build lifecycle
● pom.xml
● Directory layout
● Archetypes
● JUnit
What is JUnit?

● JUnit is an open source framework that has been designed for the
purpose of writing and running tests in the Java programming
language

● Originally written by Erich Gamma and Kent Beck

● JUnit is a regression-testing framework that developers can use to


write tests as they develop systems
How it looks

public class Sum {

int plus(int a, int b) {


return a + b;
}
}

import org.junit.Test;
public class MainTest {

@Test
public void testPlus() throws Exception {
Sum sum = new Sum();
assertEquals(4, sum.plus(2, 2));
}
}
How it looks

public class MainTest {


Sum sum;

@Before
public void setUp() throws Exception {
sum = new Sum();
}

@Test
public void testPlus() throws Exception {
assertEquals(4, sum.plus(2, 2));
}
}
Maven and JUnit

● The Surefire Plugin is used during the test phase of the build
lifecycle to execute the unit tests of an application.

● It generates reports in two different file formats:


● Plain text files (*.txt)
● XML files (*.xml)

● By default, these files are generated at


${basedir}/target/surefire-reports.
Maven and JUnit

mvn test

[INFO] Scanning for projects...


[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven jUnit sample 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]

[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ unit-test-sample ---
[INFO] Surefire report directory: /work/java/unit-test-sample/target/surefire-reports

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.epam.unittest.MainTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.044 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0


Questions ?

You might also like