I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE COURSE

1. Introduction

This quick guide shows how to build a jar with Maven while ignoring the test results.

By default, Maven runs unit tests automatically while building the project. However, there are rare cases when the tests can be skipped and we need to build the project regardless of the test results.

2. Building the Project

Let’s create a simple project where we also include a small test case:

public class TestFail {
    @Test
    public void whenMessageAssigned_thenItIsNotNull() {
        String message = "hello there";
        assertNotNull(message);
    }
}

Let’s build a jar file by executing the following Maven command:

mvn package

This will result in compiling the sources and generate a maven-0.0.1-SNAPSHOT.jar file under the /target directory.

Now, let’s change the test a bit, so the test starts to fail.

@Test
public void whenMessageAssigned_thenItIsNotNull() {
    String message = null;
    assertNotNull(message);
}

This time, when we try to run the mvn package command again, the build fails and the maven-0.0.1-SNAPSHOT.jar file isn’t created.

This means, if we have a failing test in our application, we can’t provide an executable file unless we fix the test.

So how can we solve this problem?

3. Maven Arguments

Maven has its own arguments to deal with this issue:

  • -Dmaven.test.failure.ignore=true ignores any failure that occurs during test execution
  • -Dmaven.test.skip=true would not compile the tests
  • -fn, -fae never fails the build regardless of test results

Let’s run the mvn package -Dmaven.test.skip=true command and see the results:

[INFO] Tests are skipped.
[INFO] BUILD SUCCESS

This means the project will be built without compiling the tests.

Now let’s run the mvn package -Dmaven.test.failure.ignore=true command:

[INFO] Running testfail.TestFail
[ERROR] whenMessageAssigned_thenItIsNotNull java.lang.AssertionError
[INFO] BUILD SUCCESS

Our unit test fails on assertion but the build is successful.

Finally, let’s test the -fn, -fae options. Both, package -fn and package -fae commands build the jar file and produce the BUILD SUCCESS output regardless of whenMessageAssigned_thenItIsNotNull() test fail.

In case of the multi-module project -fn option should be used. -fae continues with the module that has a failing test but skips all the dependent modules. 

4. Maven Surefire Plugin

Another convenient way to achieve our goal is to use Maven’s Surefire plugin.

For an extended overview of the Surefire plugin, refer to this article.

To ignore test fails we can simply set the testFailureIgnore property to true:

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${maven.surefire.version}</version>
    <configuration>
        <includes>
            <include>TestFail.java</include>
        </includes>
        <testFailureIgnore>true</testFailureIgnore>
    </configuration>
</plugin>

Now, let’s see the output of the package command:

[INFO]  T E S T S
[INFO] Running testfail.TestFail
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, <<< FAILURE! - in testfail.TestFail

From the running tests output, we can see the TestFail class is failing. But looking further we see that the BUILD SUCCESS message is also there and the maven-0.0.1-SNAPSHOT.jar file is compiled.

Depending on our need, we can skip running the tests at all. For that we can replace the testFailureIgnore line with:

<skipTests>true</skipTests>

Or set the command line argument -DskipTests. This will compile the test classes but skip test execution entirely.

5. Conclusion

In this article, we learned how to build our project with Maven regardless of the test results. We went through the practical examples of skipping the failing tests or excluding compilation of the tests entirely.

As usual, the complete code for this article is available over on GitHub project.

I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE LESSONS