Skip to content

Commit 1eec82d

Browse files
authored
Java JUnit5 example using Maven (#109)
* Java JUnit5 example using Maven * Add JUnit example to regression test
1 parent 9771258 commit 1eec82d

6 files changed

Lines changed: 189 additions & 0 deletions

File tree

devops/pipelines/marketplace-extension/regression-test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ parameters:
1414
- name: NUnitExample-Attachments-Wildcard
1515
format: nunit
1616
testResultsFile: '$(Build.SourcesDirectory)/examples/generated/**/TestResults.xml'
17+
- name: JUnitExample-Maven
18+
format: junit
19+
testResultsFile: '$(Build.SourcesDirectory)/examples/java/JUnitExample-maven/TestResults.xml'
1720

1821
steps:
1922
- pwsh: |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Java JUnit Example (Maven)
2+
3+
This sample illustrates java-based JUnit tests that produce JUnit results that can be published to a test plan.
4+
5+
A sample of the XML output file is included for reference.
6+
7+
## Environment
8+
9+
This project is written with the following:
10+
11+
- JDK 17
12+
- Maven 3
13+
- JUnit 5.10
14+
15+
```shell
16+
# windows administrative command prompt with choco installed
17+
choco install temurin17 maven -y
18+
```
19+
20+
## Usage
21+
22+
To run the tests and produce the Xml output:
23+
24+
```shell
25+
export JAVA_HOME=/path/to/jdk
26+
export PATH=$JAVA_HOME/bin:$PATH
27+
mvn clean test verify
28+
```
29+
30+
To publish junit results to a test plan, the following pipeline syntax matches the referenced examples:
31+
32+
```yaml
33+
- task: PublishTestPlanResults@1
34+
inputs:
35+
testResultsFiles: 'examples/java/JUnitExample-maven/TestResults.xml'
36+
testResultFormat: junit
37+
testPlan: 'Your Test Plan'
38+
testCaseMatchStrategy: auto # this is the default value
39+
testCaseProperty: TestCaseId # because the tests are using custom properties
40+
testCaseRegex: 'testCase(\d+)' # because the tests use a naming convention of '<testname>_TestCaseNNN'
41+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report-3.0.xsd" version="3.0" name="com.example.SampleTests" time="0.19" tests="7" errors="0" skipped="1" failures="1">
3+
<properties>
4+
<property name="java.specification.version" value="17"/>
5+
<property name="java.class.version" value="61.0"/>
6+
</properties>
7+
<testcase name="jUnit_passingTest_testCase4867" classname="com.example.SampleTests" time="0.062"/>
8+
<testcase name="jUnit_associateTestCaseUsingTestProperty" classname="com.example.SampleTests" time="0.008">
9+
<system-out><![CDATA[[[PROPERTY|TestCaseId=4876]]
10+
]]></system-out>
11+
</testcase>
12+
<testcase name="jUnit_skippedTest_testCase4869" classname="com.example.SampleTests" time="0.0">
13+
<skipped message="This test is skipped"/>
14+
</testcase>
15+
<testcase name="JUnit Match Test Case by Display Name" classname="com.example.SampleTests" time="0.001"/>
16+
<testcase name="jUnit_failingTest_testCase4868" classname="com.example.SampleTests" time="0.017">
17+
<failure message="This test failed intentially" type="org.opentest4j.AssertionFailedError"><![CDATA[org.opentest4j.AssertionFailedError: This test failed intentially
18+
at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:38)
19+
at org.junit.jupiter.api.Assertions.fail(Assertions.java:134)
20+
at com.example.SampleTests.jUnit_failingTest_testCase4868(SampleTests.java:19)
21+
at java.base/java.lang.reflect.Method.invoke(Method.java:569)
22+
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
23+
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
24+
]]></failure>
25+
</testcase>
26+
<testcase name="jUnit_Match_TEST_case_By_Exact_Name_cAsE_iNsenSITive" classname="com.example.SampleTests" time="0.001"/>
27+
<testcase name="jUnit_Match_Test_Case_By_Exact_Name" classname="com.example.SampleTests" time="0.001"/>
28+
</testsuite>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.example</groupId>
4+
<artifactId>junit5-sample</artifactId>
5+
<version>1.0</version>
6+
7+
<dependencies>
8+
<dependency>
9+
<groupId>org.junit.jupiter</groupId>
10+
<artifactId>junit-jupiter</artifactId>
11+
<version>5.10.0</version>
12+
<scope>test</scope>
13+
</dependency>
14+
</dependencies>
15+
16+
<build>
17+
<plugins>
18+
<plugin>
19+
<groupId>org.apache.maven.plugins</groupId>
20+
<artifactId>maven-surefire-plugin</artifactId>
21+
<version>3.1.2</version>
22+
<configuration>
23+
<useModulePath>false</useModulePath>
24+
<testFailureIgnore>true</testFailureIgnore> <!-- don't stop the build because tests failed -->
25+
26+
<!-- render @displayName in xml output-->
27+
<statelessTestsetReporter
28+
implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5Xml30StatelessReporter">
29+
<disable>false</disable>
30+
<version>3.0</version>
31+
<usePhrasedFileName>true</usePhrasedFileName>
32+
<usePhrasedTestSuiteClassName>true</usePhrasedTestSuiteClassName>
33+
<usePhrasedTestCaseClassName>true</usePhrasedTestCaseClassName>
34+
<usePhrasedTestCaseMethodName>true</usePhrasedTestCaseMethodName>
35+
</statelessTestsetReporter>
36+
</configuration>
37+
</plugin>
38+
39+
<!-- copy test results to root directory, runs when 'verify' is included in mvn target-->
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-antrun-plugin</artifactId>
43+
<version>3.1.0</version>
44+
<executions>
45+
<execution>
46+
<id>copy-test-result</id>
47+
<phase>verify</phase>
48+
<configuration>
49+
<target>
50+
<copy file="target/surefire-reports/TEST-com.example.SampleTests.xml"
51+
tofile="TestResults.xml" />
52+
</target>
53+
</configuration>
54+
<goals>
55+
<goal>run</goal>
56+
</goals>
57+
</execution>
58+
</executions>
59+
</plugin>
60+
</plugins>
61+
62+
</build>
63+
</project>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.example;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
public class SampleTests {
7+
8+
// Match Strategy: regex
9+
// These tests demonstrate using the
10+
// regex match strategy, where the TestCase
11+
// number appears in the test method name.
12+
// The `testCaseRegex` input is set to: 'testCase(\d+)'
13+
@Test
14+
public void jUnit_passingTest_testCase4867() {
15+
}
16+
17+
@Test
18+
public void jUnit_failingTest_testCase4868() {
19+
fail("This test failed intentially");
20+
}
21+
22+
@Test
23+
@org.junit.jupiter.api.Disabled("This test is skipped")
24+
public void jUnit_skippedTest_testCase4869() {
25+
// This test is skipped
26+
}
27+
28+
// Match Strategy: name
29+
// These tests demonstrate that the name of the test method
30+
// is used to match the TestCase name
31+
@Test
32+
public void jUnit_Match_Test_Case_By_Exact_Name() {
33+
}
34+
35+
@Test
36+
public void jUnit_Match_TEST_case_By_Exact_Name_cAsE_iNsenSITive() {
37+
}
38+
39+
@Test
40+
@org.junit.jupiter.api.DisplayName("JUnit Match Test Case by Display Name")
41+
public void match_testCaseByDisplayNameInsteadOfMethodName() {
42+
}
43+
44+
// Match Strategy: property
45+
// These tests demonstrate how to match TestCases using
46+
// meta-data properties. The `testCaseProperty` input is set to: 'TestCaseId'
47+
@Test
48+
public void jUnit_associateTestCaseUsingTestProperty() {
49+
// custom properties can be set by writing to the console
50+
System.out.println("[[PROPERTY|TestCaseId=4876]]");
51+
}
52+
53+
}

0 commit comments

Comments
 (0)