Maven & Junit Introduction
Maven & Junit Introduction
Maven & Junit Introduction
Agenda
● Introduction
● Build lifecycle
● pom.xml
● Directory layout
● Archetypes
● JUnit
Introduction
● Introduction
● Build lifecycle
● pom.xml
● Directory layout
● Archetypes
● JUnit
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
https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference
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.
● 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
<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>
<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>
<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?
● 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
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
@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.
mvn test
[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 :