Skip to content

Commit 10abc7e

Browse files
committed
create the basic first test and reporting build
1 parent d036c8b commit 10abc7e

4 files changed

Lines changed: 171 additions & 2 deletions

File tree

‎README.md‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
1-
# startUsingJavaJUnit5
2-
Basic setup for an up to date java with Junit 5
1+
# Start Using Java JUnit5
2+
3+
Basic setup for an up to date java maven project with Junit 5.
4+
5+
Requirements:
6+
7+
- Install Java DSK (anything above v 1.9)
8+
- Install maven
9+
10+
Run:
11+
12+
- `mvn test`
13+
14+
In the `/target/site/jacoco/` folder the `index.hmtl` file should have the JUnit execution and coverage report.
15+

‎pom.xml‎

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>uk.co.compendiumdev</groupId>
8+
<artifactId>junitexamples</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<junit.jupiter.version>5.6.2</junit.jupiter.version>
14+
<!-- configure the compiler plugin via properties rather than plugin -->
15+
<!-- https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html -->
16+
<maven.compiler.source>1.9</maven.compiler.source>
17+
<maven.compiler.target>1.9</maven.compiler.target>
18+
19+
<!-- to generate html junit reports for test phase -->
20+
<maven-site-plugin.version>3.7.1</maven-site-plugin.version>
21+
<maven.surefire.report.version>3.0.0-M4</maven.surefire.report.version>
22+
23+
<!-- to create the code coverage report -->
24+
<jacoco-version>0.8.10</jacoco-version>
25+
</properties>
26+
27+
28+
<dependencies>
29+
<!-- adding the api and engine allow JUnit 5 to work -->
30+
<dependency>
31+
<groupId>org.junit.jupiter</groupId>
32+
<artifactId>junit-jupiter-api</artifactId>
33+
<version>${junit.jupiter.version}</version>
34+
<scope>test</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.junit.jupiter</groupId>
38+
<artifactId>junit-jupiter-engine</artifactId>
39+
<version>${junit.jupiter.version}</version>
40+
<scope>test</scope>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<plugins>
46+
<!--
47+
Maven Surefire Plugin to run the Unit tests
48+
https://maven.apache.org/surefire/maven-surefire-plugin/usage.html
49+
-->
50+
<plugin>
51+
<groupId>org.apache.maven.plugins</groupId>
52+
<artifactId>maven-surefire-plugin</artifactId>
53+
<version>${maven.surefire.report.version}</version>
54+
</plugin>
55+
56+
<!--
57+
Maven Site Plugin to create the HTML site
58+
https://maven.apache.org/surefire/maven-surefire-plugin/usage.html
59+
-->
60+
<plugin>
61+
<groupId>org.apache.maven.plugins</groupId>
62+
<artifactId>maven-site-plugin</artifactId>
63+
<version>${maven-site-plugin.version}</version>
64+
</plugin>
65+
66+
67+
<!-- add code coverage and make sure it covers both Junit4 and Junit5 -->
68+
<plugin>
69+
<groupId>org.jacoco</groupId>
70+
<artifactId>jacoco-maven-plugin</artifactId>
71+
<version>${jacoco-version}</version>
72+
<!-- defaults to target/jacoco.exec -->
73+
<executions>
74+
<!-- allow Jacoco to run -->
75+
<execution>
76+
<goals>
77+
<goal>prepare-agent</goal>
78+
</goals>
79+
</execution>
80+
<!--
81+
Generate the html report for the test phase
82+
https://www.eclemma.org/jacoco/trunk/doc/report-mojo.html
83+
-->
84+
<execution>
85+
<id>report</id>
86+
<!-- generate coverage report for test phase -->
87+
<!-- target/site/jacoco -->
88+
<phase>test</phase>
89+
<goals>
90+
<goal>report</goal>
91+
</goals>
92+
</execution>
93+
</executions>
94+
</plugin>
95+
</plugins>
96+
</build>
97+
98+
<!-- `mvn clean test site` to generate the junit html report-->
99+
<reporting>
100+
<plugins>
101+
<!--
102+
Maven Reports Plugin to generate the HTML reports
103+
https://maven.apache.org/surefire/maven-surefire-report-plugin/index.html
104+
-->
105+
<plugin>
106+
<groupId>org.apache.maven.plugins</groupId>
107+
<artifactId>maven-surefire-report-plugin</artifactId>
108+
<version>${maven.surefire.report.version}</version>
109+
</plugin>
110+
111+
<!-- generate the coverage report using Jacoco -->
112+
<plugin>
113+
<groupId>org.jacoco</groupId>
114+
<artifactId>jacoco-maven-plugin</artifactId>
115+
<reportSets>
116+
<reportSet>
117+
<reports>
118+
<!-- select non-aggregate reports -->
119+
<report>report</report>
120+
</reports>
121+
</reportSet>
122+
</reportSets>
123+
</plugin>
124+
</plugins>
125+
</reporting>
126+
127+
</project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package co.compendiumdev.junit;
2+
3+
public class MyFirstClass {
4+
5+
public static int add(int num1, int num2){
6+
return num1 + num2;
7+
}
8+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package co.compendiumdev.junit;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class MyFirstTest {
7+
8+
@Test
9+
public void checkThatJavaMavenAndJUnitAreInstalled(){
10+
Assertions.
11+
assertTrue(true,
12+
"this test should pass if Java, JUnit and Maven are installed");
13+
}
14+
15+
@Test
16+
public void checkThatCodeCoverageReportWorks(){
17+
Assertions.
18+
assertEquals(4, MyFirstClass.add(1, 3),
19+
"this test should generate code coverage");
20+
}
21+
}

0 commit comments

Comments
 (0)