How to Skip Unit Tests in Maven

This tutorial will teach you how to build a Maven-based Spring Boot project and skip Unit tests.

Sometimes, when working on a project, we need to build and run it even though our Unit test is failing. When there is a flailing Unit test, the Maven package and install commands will also fail.  This is because both of these commands execute Unit tests.

To skip Unit tests when building a Maven-based Spring Boot project, use one of the following ways.

Skip Unit Test With -Dmaven.test.skip=true

One of the ways to skip Unit tests when building a Maven project is to use -Dmaven.test.skip=true. For example, to package a maven project without running Unit tests, you will issue the following command:

mvn package -Dmaven.test.skip=true

or if you use Maven Install, then:

mvn install -Dmaven.test.skip=true

To disable Unit tests using POM.XML add maven.test.skip to properties XML element.

<properties>
        <maven.test.skip>true</maven.test.skip>
</properties>

If You Use maven-surefire-plugin

If you use maven-surefire-plugin then when running the Maven command in the terminal window, you can skip Unit tests the following way:

mvn package -DskipTests

or if you use Maven install, then,

mvn install -DskipTests

Alternatively, to use -DskipTests in a terminal window when running a command, you can configure to skip Unit tests in POM.xml file this way:

   
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M3</version>
    <configuration>
        <skipTests>true</skipTests>
    </configuration>
</plugin>

Video Tutorial

Conclusion

I hope this tutorial was helpful to you.

If you want to learn more about Maven and you enjoy learning by following a series of step-by-step video lessons, then look at the below list of video courses that teach Maven.

Ready to take your unit testing skills to the next level? Discover a treasure trove of tutorials on the Testing Java Code page, where you’ll learn how to effectively structure your test methods, ensuring comprehensive test coverage and efficient debugging.


Leave a Reply

Your email address will not be published.