Maven

August 5, 2024
Trunk Team

What is Maven?

Maven is an open-source build automation and project management tool that helps developers build, publish, and deploy projects. It's written in Java and is primarily used for Java projects, but can also be used for projects written in other languages like C#, Ruby, and Scala. It is often used to run Java unit tests.

Enabling XML Output

When a Maven project is tested with mvn test it does not automatically produce JUnit XML output. You can enable XML output with the Surefire plugin. To enable it, add the following to the plugins section of your pom.xml file. This process is the same whether you use test using JUnit or TestNG.

1<plugin>
2 <groupId>org.apache.maven.plugins</groupId>
3 <artifactId>maven-surefire-plugin</artifactId>
4 <version>3.3.1</version>
5</plugin>

and then run

1mvn test

The output will be in the target/surefire-reports directory.

Test Suite naming

You can change the output filename with the <reportsDirectory> property.

1<plugin>
2 <groupId>org.apache.maven.plugins</groupId>
3 <artifactId>maven-surefire-report-plugin</artifactId>
4 <version>3.3.1</version>
5 <configuration>
6 <reportsDirectory>coolthing</reportsDirectory>
7 </configuration>
8</plugin>

You can see a full list of configuration options here.

JUnit

JUnit uses annotations to define test suites. For example, here's a test suite named My Test Suite.

1@Suite
2@SuiteDisplayName("My Test Suite")
3public class JUnitTestSuite {
4 ...
5}

TestNG

TestNG uses a testing.xml file to define test cases. You can specify test suite names in testing.xml.

For example, here's a test suite named Suite1.

1<suite name="Suite1" verbose="1">
2 <test name="Nopackage">
3 <classes>
4 <class name="NoPackageTest"/>
5 </classes>
6 </test>
7
8 <test name="Regression1">
9 <classes>
10 <class name="test.sample.ParameterSample"/>
11 <class name="test.sample.ParameterTest"/>
12 </classes>
13 </test>
14</suite>

Handling Retries

Maven can retry failed tests using a command line option.

1mvn -D surefire.rerunFailingTestsCount=2 test

See the Rerun Failing Tests page for more details.

Other Configuration

See an example of running Maven inside of a GitHub action here.

Try it yourself or
request a demo

Get started for free

The DevOps Assistant
for self-healing CI