Main Tutorials

Maven – How to skip unit test

In Maven, you can define a system property -Dmaven.test.skip=true to skip the entire unit test.

By default, when building project, Maven will run the entire unit tests automatically. If any unit tests is failed, it will force Maven to abort the building process. In real life, you may STILL need to build your project even some of the cases are failed.

In this article, we will show you a few ways to skip the unit test.

1. maven.test.skip=true

1.1 To skip unit tests, uses this argument -Dmaven.test.skip=true

Terminal

$ mvn package -Dmaven.test.skip=true
#no test

1.2 Or defined in pom.xml

pom.xml

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

$ mvn package
#no test

2. Maven Surefire Plugin

2.1 Alternatively, use this -DskipTests in surefire plugin.

Terminal

$ mvn package -DskipTests
#no test

2.2 Or this.

pom.xml

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

2.3 To skip some test classes.

pom.xml

	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-surefire-plugin</artifactId>
		<version>3.0.0-M1</version>
		<configuration>
			<excludes>
				<exclude>**/TestMagic*.java</exclude>
				<exclude>**/TestMessage*.java</exclude>
			</excludes>
		</configuration>
    </plugin>

3. Maven Profile

3.1 Create a custom profile to skip the unit test.

pom.xml

	<profiles>
        <profile>
			<id>xtest</id>
			<properties>
				<maven.test.skip>true</maven.test.skip>
			</properties>
		</profile>
    </profiles>

3.2 Active the profile with a -P option.

Terminal

$ mvn package -Pxtest
#no test

References

  1. Maven Surefire Plugin
  2. Inclusions and Exclusions of Tests
  3. Skipping Tests
  4. Maven Profiles example
  5. How to run unit test with Maven

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
27 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Monica
10 years ago

Wrong. The “unit tests will be ignored” is just wrong. The Maven guys whine that it is too hard to get Maven to ignore and not compile unit tests so the -DskipTests only doesn’t execute the unit tests. It doesn’t ignore them and not compile.

It’s amazing how childish Maven people are. They could have fixed their problem in less time than it takes them to whine about how it is difficult to implement. Grow up.

mkyong
7 years ago
Reply to  Monica

Uses ‘maven.test.skip’ , it will not compile any unit tests.

Laurent C.
10 years ago

I can argue about this commandline but this blog made it better:

Source: Learning by Experience

Recently came across this the existance of the -DskipTests argument while running maven.

From the userguide:

You can also skip the tests via command line by executing the following command:

mvn install -DskipTests

If you absolutely must, you can also use the maven.test.skip property to skip compiling the tests. maven.test.skip is honored by Surefire, Failsafe and the Compiler Plugin.

mvn install -Dmaven.test.skip=true

Skiptests is a feature of surefire, while -Dmaven.test.skip is a feature of maven itself.

mkyong
7 years ago
Reply to  Laurent C.

Thanks for your info,

Ulrike
3 years ago

Thanks mkyong. I found your tutorial very helpful. Have a great 2021!

Bee
1 year ago

Is there a way to do -DskipTests with profiles? I want to build tests but not run them.

Last edited 1 year ago by Bee
vivek
2 years ago

How to skip junit test if programmatically running spring boot application as follows:

String args[] = {};
SpringApplication app = new SpringApplication(SpringBootApplicationClassname.class);
ConfigurableApplicationContext context = app.run(args);

Petros
2 years ago

Is there a way to skip a specific package when we try to run the command: mvn clean install

Assume that I have multiple packages with different test cases and for some reason I want to run all packages except from 1 package. Can this be done?

Thank you

Mradul Yadav
3 years ago

Thanks 🙂

marc
4 years ago

working in 2020 xd

Murphy
5 years ago

Hello, what does -D mean? anyone knows?

Bilal
5 years ago
Reply to  Murphy

I think it stands for “define”, use as -D or –define to set system properties these can declared in the profile or in properties as shown above. see here for more info http://maven.apache.org/ref/3.1.0/maven-embedder/cli.html

Dave
6 years ago

Thanks! I am building some open source project that expects tests to be run in the context of a container so they fail on my machine, and in fact I am not editing the code just building the package so there is no need for me to test the code when it is already build successful tests passing in github. Thanks for the surefire method which works for me, the maven way didn’t work for me, I got this error: Unknown lifecycle phase “.test.skip=true”.

NIKHILESH CHAURASIA
8 years ago

Hey MK,

Can you please guide me if there is any way to skip only single TEST class out of 20 test classes while “mvn clean package”. Because in my case only once test case is failing.

Thanks

randy.su
10 years ago

More request, how to skip all steps except deploy when run “mvn tomcat:depoy”? (as I already run “mvn:install”). Actually I want to know how to skip some steps which have been done before.

Sean
10 years ago

Hi mkyong

Your posts are really helpful! Thanks!

Sean
10 years ago

Hi Mkyong

Your posts are great! really helpful!

Aleksandar Bosancic
10 years ago

Hello MK, I think it’s better solution for skip test use the properties

3.0.5.RELEASE
……
true

Thanks,
Aleksandar

Aleksandar Bosancic
10 years ago

Sorry, ‘pre’ does not work so good :), here is config

3.0.5.RELEASE
…..
true

alka
10 years ago

Waw! thx A lot master! your tip make my day more easy.

Scott
10 years ago

Mykong,

using true in the pom causes Maven to skip test execution. Is there any way to tell Maven to not compile the tests as well using a element in the pom instead of command line? Something such as true

Thanks.

Scott
10 years ago
Reply to  Scott

Sorry, did not use the source code comment:

true

Pete
11 years ago

Well, the test was one we could safely ignore:
testFindDomainBounds in the TimeSeriesCollectionTests returned:
expected: but was:
So we can assume it’s a basic rounding error, but very minor so safe to ignore.
Not sure if it’s a Mac thing, perhaps a 64-bit issue but the test should be updated to round somewhat.

Pete
11 years ago

Thanks alot mkyong. I had one unit test out of over 2000 fail in jFreeChart! Annoying, but your tip saved the day. Thanks again…
Pete
Melbourne, Australia

Smithf77
5 years ago

Right now it looks like WordPress is the top blogging platform available right now. from what I’ve read Is that what you’re using on your blog? bkcceddgfdddcebe