How to ignore test failures while building projects in maven?

Sometimes we measure test failures to measure how close we are to completing our project. The default behavior of maven is to stop the current build if it encounters any test failures. This post will discuss how we can ignore the test failures and get the build output.

Failing test cases

Let’s see what happens when a test case fails while building a project using maven.

By default, maven will execute every test case under the src/test/java directory. We will make one AppTest.java class in src/test/java and fail one of its cases.

AppTest.java

package org.website.codekru;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
 * Unit test for simple App.
 */
public class AppTest {

	@Test
	public void test1() {
		System.out.println("Executing the first test case");
		Assertions.assertTrue(true);
	}

	@Test
	public void test2() {
		System.out.println("Executing the second test case");
		Assertions.assertTrue(false); // failing this test case
	}
}

Our project structure is –

Maven Project structure
  • We will be using  JUnit Jupiter API to make our unit test cases. Please visit this link to find the JUnit Jupiter API maven dependencies list.
  • And we will also use the maven surefire plugin to facilitate the execution of test cases.

Here is our pom.xml file –

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>org.website.codekru</groupId>
	<artifactId>DemoProject</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>DemoProject</name>
	<url>http://maven.apache.org</url>

	<dependencies>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-api</artifactId>
			<version>5.8.2</version>

		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>3.0.0-M7</version>
			</plugin>
		</plugins>
	</build>

</project>

Now, run the “mvn install” command to build the project.

maven build failure because a test case failed

We can see that the build is halted because one of the unit test cases failed.

How to ignore the test failures?

We can do that by –

Modifying the pom.xml file

We have to add the below lines in our pom.xml file to ignore the test failures.

<configuration>
   <testFailureIgnore>true</testFailureIgnore>
</configuration>

Now, our updated pom.xml would look like this –

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>org.website.codekru</groupId>
	<artifactId>DemoProject</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>DemoProject</name>
	<url>http://maven.apache.org</url>

	<dependencies>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-api</artifactId>
			<version>5.8.2</version>

		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>3.0.0-M7</version>
				
				<configuration>
					<testFailureIgnore>true</testFailureIgnore>
				</configuration>
				
			</plugin>
		</plugins>
	</build>

</project>

Run the “mvn install” command again, and your project will produce the build output.

build made even after failing the test case

The test case still failed, but the build was successfully created for the project.

Using the command line arguments

If you don’t want to modify the pom.xml file, we can achieve the same functionally by using the command line arguments. We can use the below command to ignore the test failures –

mvn install -Dmaven.test.failure.ignore=true

If we set maven.test.failure.ignore to true, it will ignore all the test failures.

What if you used both of the methods? Which one has the higher priority in ignoring the failed cases?

The answer is pom.xml.

  • If the command-line argument is set to false (-Dmaven.test.failure.ignore=false) and pom.xml has set its configuration to true ( <testFailureIgnore>true</testFailureIgnore> ). Then the cases would be ignored
  • Suppose we set the command-line argument as true and the pom.xml configuration to false. Then it won’t ignore the test cases, and the build will fail.

This clearly shows that pom.xml has a higher priority than command-line arguments.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Related Articles –

Liked the article? Share this on

Leave a Comment

Your email address will not be published.