1. Overview

In this quick tutorial, we'll look at how we can run a single test using Maven.

2. Unit Tests

We'll first look at the unit tests.

2.1. Maven Plugin

Let's add the maven-surefire-plugin which runs the unit tests:


The version - 2.22.2 - is important because some of the explained features aren't available in the earlier releases of Surefire.

2.2. Run a Single Unit Test

The Surefire Plugin provides the test configuration property which determines the file name of test classes.

Now we'll configure it from the command line using the test property:

mvn -Dtest=SampleTest test

Here, we just provide the test class name. Then Surefire turns SampleTest into **/SampleTest.java. As a result, if there is a SampleTest class on any part of the classpath, Surefire runs it.

When we add the java extension, Surefire ignores it:

mvn -Dtest=SampleTest.java test

This produces the same result as the previous example. Because Surefire itself adds java to the final file pattern.

We can also specify the fully qualified class name:

mvn -Dtest=com.javabyexamples.maven.plugins.surefire.SampleTest test

In this case, we're more specific about the test class, since we're specifying both the name and the package. Consequently, this can produce a different result compared to the previous examples. For instance, if we had multiple SampleTest classes in different packages, -Dtest=SampleTest would run all of them. In contrast, -Dtest=com.javabyexamples.maven.plugins.surefire.SampleTest would run only one of them - the one with the matching package.

Now let's run multiple test classes:

mvn -Dtest=SampleTest,AnotherTest test

Here, we're listing different classes separated by commas.

We can also specify multiple test classes using a pattern:

mvn -Dtest=Sam*Test test

Surefire will run all test classes that start with Sam and end with Test.

So far we only dealt with the test classes, not the test methods. Surefire also allows us to run a single test method:

mvn -Dtest=SampleTest#shouldRun test

In the end, only the shouldRun method in SampleTest will run.

We can also specify multiple test methods separated by pluses (+):

mvn -Dtest=SampleTest#shouldRun+shouldFail test

Surefire will run the shouldRun and shouldFail methods.

3. Integration Tests

Let's continue with the integration tests.

3.1. Maven Plugin

Let's add the maven-failsafe-plugin which runs the integration tests:


Similar to the Surefire Plugin, the version is important since some features may be missing in the earlier releases. Also, we have a sample executions configuration which can be modified according to our needs .

3.2. Run a Single Integration Test

The Failsafe Plugin also provides a test configuration property that defines the integration tests. Moreover, we can set it through the user property it.test:

mvn -Dit.test=SampleIT verify

The Failsafe Plugin will take the SampleIT and create the **/SampleIT.java pattern. As a result, all SampleIT classes will be run.

The rules that are valid for Surefire are also valid for Failsafe. But let's go over them again one by one.

Since Failsafe ignores the java extension, we don't need to specify it:

mvn -Dit.test=SampleIT.java verify

Similar to Surefire, we can specify the fully qualified name:

mvn -Dit.test=com.javabyexamples.maven.plugins.failsafe.SampleIT verify

We can define multiple test classes separating them by commas:

mvn -Dit.test=SampleIT,AnotherIT verify

Or alternatively, we can use patterns:

mvn -Dit.test=Sam*IT verify

Instead of a single test class, we can run a single test method:

mvn -Dit.test=SampleIT#shouldSayHello verify

Similarly, we can specify multiple test methods:

mvn -Dit.test=SampleIT#shouldSayHello+shouldSayBye verify

4. Summary

In this tutorial, we've looked at how we can run a single test class or a single test method using Maven. For this purpose, we investigated both the Maven Surefire Plugin and the Maven Failsafe Plugin.

Finally, check out the source for all examples over on Github.