Running specific test cases in Maven helps developers execute only the required tests instead of the entire test suite, saving time and improving productivity. It is especially useful during debugging and development when focusing on particular functionalities.
- Use
-Dtest=ClassNameto run a single test class. - Use
-Dtest=ClassName#methodNameto run a specific test method.
Run a single test class
mvn test -Dtest=ClassName
Run a single test method
mvn test -Dtest=ClassName#methodName
Implementation to Run a Single Test or Method With Maven
Here are the step by step Implementation to run a single test
Step 1: Create a Maven Project
Create a simple maven project by using your favourite tool. Here, we use STS IDE By using required dependencies.
Dependencies:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.app</groupId>
<artifactId>mavencommends</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mavencommends</name>
<description>Spring Reactive</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Project Structure:

Step 2: Create Test Class
Navigate to the src/test/java folder, where a default test class is already available. Add your test logic inside this class, which typically includes methods like testMethod1 and testMethod2.
MavencommendsApplicationTests.java:
package com.app;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class MavencommendsApplicationTests {
@Test
void contextLoads() {
}
@Test
public void testMethod1() {
assertTrue(true);
}
@Test
public void testMethod2() {
assertTrue(true);
}
}
Step 3: Configure Dependencies
Make sure your pom.xml is setup to use JUnit 5. If you don't have below we provide that dependency copy into your pom.xml then update the Project.
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.1</version>
<scope>test</scope>
</dependency>
</dependencies>
Step 4: Run Entire Test Class
The following command will run all the test methods in the MavencommendsApplicationTests class. Here we run entire test methods in the test class.
mvn -Dtest=MavencommendsApplicationTests test
Output:

Maven Build Success:

Step 5: Run Single Test Method
Now we run a single method in the test class. To run a specific test method, use the following command. Here we run one specific test method from test class by using below command.
mvn -Dtest=MavencommendsApplicationTests#testMethod1 test
Output:

Maven Single Test Method Build Success:
