EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Maven Tutorial Maven Run Single Test
 

Maven Run Single Test

Updated April 12, 2023

Maven Run Single Test

 

 

Introduction to Maven Run Single Test

Maven run single test is used to run a single test on a specified project implemented in maven. While running the single test, we need to use the surefire plugin because this plugin will contain only one test of the goal. We need to add this plugin to the pom.xml file. By using the default configuration of the maven project, we can execute all the configurations of the maven project using the mvn test command.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Key Takeaways

  • It is used to run tests on a single class or method on which we have defined the test method.
  • We can also run tests on all projects using the mvn test command. While executing a test on a single method, it will execute a test on a specified method.

Overview

When executing a single class or method, we are using maven to run a single test. Suppose we need to run a single test from all the tests we have configured into the project, then we need to define the specific test name at the time of running the test on the specified application.

We need to run this test into the project directory where our pom.xml file will reside. To run all tests in maven is very easy. We need to run all the tests using the mvn command. We can run all the tests using the command line where our project repository resides. To run a single test is very easy in the maven application.

How to Run Single Test in Maven?

The below steps show how we can run the single test in maven as follows.

For running a single test, we need to create the project template, we are creating the project template name maven_test as follows.

1. In this step we are creating the project of maven_test by using spring initializer. Below we are providing the name of the project as maven_test.

Group name – com.example
Artifact – maven_test
Name – maven_test
Packaging – jar
Java version – 8
Language – java

Maven Run Single Test 1

2. In the second step we are opening the project template of the maven test project by using the spring tool suite as follows.

Maven Run Single Test 2

3. After opening the maven test project, in the below example we can see that we are checking the structure of the project.

checking the structure of project

4. After checking the structure of the project now in this step we are adding the surefire plugin to run the single test on the maven project as follows.

Code:

<plugin>
  <groupId> org.apache.maven.plugins </groupId>
  <artifactId> maven-surefire-plugin </artifactId>
  <version> 2.22.2 </version>
</plugin>
<plugin>
  <groupId> org.apache.maven.plugins </groupId>
  <artifactId> maven-failsafe-plugin </artifactId>
  <version> 2.22.2 </version>
</plugin>

Output:

Maven Run Single Test 4

5. After adding the test plugin into the pom.xml file now in this step we are creating the simple test classes for running the single test as follows.

Code:

class maven_test1 {
@Test
   void test1() {
   logger.info("Run test1");
   }
}
class maven_test2 {
@Test
void test2() {
   logger.info("Run test2");
}
@Test
void test3() {
   logger.info("Run test3");
}
@Test
void test4() {
   logger.info("Run test4");
}
@Test
void test5() {
   logger.info("Run test5");
  }
}

Output:

simple test classes

6. After creating the simple test class now we can run the single test class on above-created class as follows. In the below example, we are executing the test on maven_test1 class as follows.

Code:

mvn test –Dtest = "maven_test1"

Output:

Maven Run Single Test 6

Maven Run Single Test Project

We can run tests on all the projects by using maven. To run the test on all projects is very simple as compared to run a test on a single method or class. We are using the maven_test project to run the single test project.

Below is the project structure of the maven_test project as follows:

Maven Run Single Test 7

In the below example, we are running the test on the project as follows. To run the test on a project we have no need to define any parameter. We are only providing the below command to run tests on the maven project. We need to provide only the mvn test command while running the test on the project.

Code:

mvn test

Output:

Maven Run Single Test 8

We can also execute the maven test on a project by using the spring tool suite. The below example shows that execute the test as follows.

spring tool suite

Maven Run Single Test 10

Maven Run Single Test Class

We can run tests on a single class by using maven. To run the test on a single class is very simple as compared to run a test on a single method. We are using maven_test project to run the single test class as follows. Below is the project structure of the maven_test project as follows.

Maven Run Single Test 11

In the below example, we are running the test on a single class as follows. To run the test on class we have to need to define –Dtest parameter, with this parameter we are providing the name of the class in which we are executing the test as follows. In the below example, we are executing a test on maven_test1 class.

Code:

mvn test –Dtest = "maven_test1"

Output:

Maven Run Single Test 12

In the above example, we have executed the test on maven_test1 class now we are executing the test on maven_test2 class as follows.

Code:

mvn test –Dtest = "maven_test2"

Output:

Maven Run Single Test 13

Configuration

To run the single test configuration, we need to add the surefire plugin in the pom.xml file.  The Surefire plugin is used to run the test on our project. The below example shows to add the surefire plugin in our project.

Code:

<plugin>
  <groupId> org.apache.maven.plugins </groupId>
  <artifactId> maven-surefire-plugin </artifactId>
  <version> 2.22.2 </version>
</plugin>

Output:

Maven Run Single Test 14

After adding the surefire plugin, now we are creating the class to run it as follows.

Code:

class maven_test1 {
@Test
void test1() {
logger.info("Run test1");
}
}
class maven_test2 {
@Test
void test1() {
logger.info("Run test2");
}
void test2() {
logger.info("Run test3");
}
}

Output:

After adding the surefire plugin

After configuring the single test now in this step we are running a single test on the test1 method as follows.

Code:

mvn test -Dtest="maven_test2#test1*"

Output:

test1 method

After running the test on test1 method, now in this step, we are running a single test on the test2 method.

Code:

mvn test -Dtest="maven_test2#test2*"

Output:

Maven Run Single Test 17

FAQ

Given below are the FAQs mentioned:

Q1. What is the use to run maven single test?

Answer: It is used to run tests on specified methods or class. We need to define the method while running the maven single test in our project.

Q2. What is the use of –Dtest parameter in the maven run single test?

Answer: The –Dtest parameter is used to run a single class or method which we have used in our class.

Q3. How to run the whole project by using maven?

Answer: We are using the maven test command to test all the projects by using maven, we need to define all the projects.

Conclusion

While running the single test we need to use surefire plugin because this plugin will contain only one test of the goal. We need to add this plugin to the pom.xml file. At the time of executing a single class or single method same time, we are using it.

Recommended Articles

We hope that this EDUCBA information on “Maven Run Single Test” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. Maven Build Command
  2. Maven WAR Plugin
  3. Maven Profile
  4. Maven Versions

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW