Master the most popular testing framework for Java, through the Learn JUnit course:
Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.
Get started with mocking and improve your application tests using our Mockito guide:
Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.
Get started with understanding multi-threaded applications with our Java Concurrency guide:
Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:
Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.
But these can also be overused and fall into some common pitfalls.
To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:
Get started with Spring and Spring Boot, through the Learn Spring course:
>> LEARN SPRINGExplore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:
Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.
I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.
You can explore the course here:
Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.
Get started with Spring Data JPA through the guided reference course:
Refactor Java code safely โ and automatically โ with OpenRewrite.
Refactoring big codebases by hand is slow, risky, and easy to put off. Thatโs where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.
Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions โ one for newcomers and one for experienced users. Youโll see how recipes work, how to apply them across projects, and how to modernize code with confidence.
Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.
1. Overview
Usually, we execute tests during a Maven build using the Maven surefire plugin.
This tutorial will explore how to use this plugin to run a single test class or test method.
2. Introduction to the Problem
The Maven surefire plugin is easy to use. It has only one goal: test.
Therefore, with the default configuration, we can execute all tests in the project by the command mvn test.
Sometimes, we may want to execute one single test class or even one single test method.
In this tutorial, weโll take JUnit 5 as the testing provider example to address how to achieve it.
3. The Example Project
To show the test results in a more straightforward way, letโs create a couple of simple test classes:
class TheFirstUnitTest {
// declaring logger ...
@Test
void whenTestCase_thenPass() {
logger.info("Running a dummyTest");
}
}
class TheSecondUnitTest {
// declaring logger ...
@Test
void whenTestCase1_thenPrintTest1_1() {
logger.info("Running When Case1: test1_1");
}
@Test
void whenTestCase1_thenPrintTest1_2() {
logger.info("Running When Case1: test1_2");
}
@Test
void whenTestCase1_thenPrintTest1_3() {
logger.info("Running When Case1: test1_3");
}
@Test
void whenTestCase2_thenPrintTest2_1() {
logger.info("Running When Case2: test2_1");
}
}
In the TheFirstUnitTest class, we have only one test method. However, TheSecondUnitTest contains four test methods. All our method names are following the โwhenโฆthenโฆโ pattern.
To make it simple, weโve made each test method output a line indicating the method is being called.
Now if we run mvn test, all tests will be executed:
$ mvn test
...
[INFO] Scanning for projects...
...
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.baeldung.runasingletest.TheSecondUnitTest
16:58:16.444 [main] INFO ...TheSecondUnitTest - Running When Case2: test2_1
16:58:16.448 [main] INFO ...TheSecondUnitTest - Running When Case1: test1_1
16:58:16.449 [main] INFO ...TheSecondUnitTest - Running When Case1: test1_2
16:58:16.450 [main] INFO ...TheSecondUnitTest - Running When Case1: test1_3
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.065 s - in com.baeldung.runasingletest.TheSecondUnitTest
[INFO] Running com.baeldung.runasingletest.TheFirstUnitTest
16:58:16.453 [main] INFO ...TheFirstUnitTest - Running a dummyTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 s - in com.baeldung.runasingletest.TheFirstUnitTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0
[INFO]
...
Next, letโs tell Maven to execute only specified tests.
4. Execute a Single Test Class
The Maven surefire plugin provides a test parameter that we can use to specify test classes or methods we want to execute.
If we want to execute a single test class, we can execute the command mvn test -Dtest=โTestClassNameโ.
For instance, we can pass -Dtest=โTheFirstUnitTestโ to the mvn command to execute the TheFirstUnitTest class only:
$ mvn test -Dtest="TheFirstUnitTest"
...
[INFO] Scanning for projects...
...
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.baeldung.runasingletest.TheFirstUnitTest
17:10:35.351 [main] INFO com.baeldung.runasingletest.TheFirstUnitTest - Running a dummyTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.053 s - in com.baeldung.runasingletest.TheFirstUnitTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
...
As the output shows, only the test class weโve passed to the test parameter is executed.
5. Execute a Single Test Method
Additionally, we can ask the Maven surefire plugin to execute a single test method by passing -Dtest=โTestClassName#TestMethodNameโ to the mvn command.
Now letโs execute the whenTestCase2_thenPrintTest2_1() method in the TheSecondUnitTest class:
$ mvn test -Dtest="TheSecondUnitTest#whenTestCase2_thenPrintTest2_1"
...
[INFO] Scanning for projects...
...
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.baeldung.runasingletest.TheSecondUnitTest
17:22:07.063 [main] INFO ...TheSecondUnitTest - Running When Case2: test2_1
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.057 s - in com.baeldung.runasingletest.TheSecondUnitTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
...
As we can see, this time, weโve executed only the specified test method.
6. More About the test Parameter
So far, weโve shown how to execute a single test class or test method by providing the test parameter value accordingly.
Actually, the Maven surefire plugin allows us to set the value of the test parameter in different formats to execute tests flexibly.
Next, weโll show some commonly used formats:
- Execute multiple test classes by name: -Dtest=โTestClassName1, TestClassName2, TestClassName3โฆโ
- Execute multiple test classes by name pattern: -Dtest=โ*ServiceUnitTestโ or -Dtest=โThe*UnitTest, Controller*Testโ
- Specify multiple test methods by name: -Dtest=โClassName#method1+method2โณ
- Specify multiple method names by name pattern: -Dtest=โClassName#whenSomethingHappens_*โ
Finally, letโs see one more example.
Letโs say we only want to execute all โwhenTestCase1โฆโ methods in the TheSecondUnitTest class.
So, following the pattern weโve talked about above, we hope that -Dtest=โTheSecondUnitTest#whenTestCase1*โ will do the job:
$ mvn test -Dtest="TheSecondUnitTest#whenTestCase1*"
...
[INFO] Scanning for projects...
...
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.baeldung.runasingletest.TheSecondUnitTest
17:51:04.973 [main] INFO ...TheSecondUnitTest - Running When Case1: test1_1
17:51:04.979 [main] INFO ...TheSecondUnitTest - Running When Case1: test1_2
17:51:04.980 [main] INFO ...TheSecondUnitTest - Running When Case1: test1_3
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.055 s - in com.baeldung.runasingletest.TheSecondUnitTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
...
As we expected, only the three test methods matching the specified name pattern have been executed.
7. Conclusion
The Maven surefire plugin provides a test parameter that allows us a lot of flexibility in choosing which tests we want to execute.
In this article, weโve discussed some commonly used value formats of the test parameter.
Also, weโve addressed through examples how to run only specified test classes or test methods with Maven.
