EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Maven Run Single Test

Secondary Sidebar
Maven Tutorial
  • Maven Basic and advance
    • What is Maven
    • How to Install Maven
    • Maven Commands
    • Maven GroupId
    • Maven WAR Plugin
    • Maven Build Command
    • Maven Failsafe Plugin
    • Maven Profile
    • IntelliJ Maven
    • Maven Enforcer Plugin
    • Maven Javadoc Plugin
    • Maven WAR Plugin
    • Maven Build Command
    • Maven GroupId
    • Maven Force Update
    • Maven Encrypt Password
    • Maven Environment Variables
    • AssertJ Maven
    • Maven Run Single Test
    • Maven kafka-clients
    • Maven Quickstart Archetype
    • Maven Install Dependencies
    • Maven XMLBeans
    • Maven Local Repository
    • Maven Versions
    • Maven Jar Plugin
    • Maven Assembly Plugin
    • Maven exec plugin
    • Maven Central Repository
    • Maven Surefire
    • Maven Deploy
    • Maven Phases
    • Maven Archetype
    • Maven Skip Test
    • Maven Dependency Scope
    • Maven Shade Plugin
    • Maven Repository Spring
    • Maven Eclipse Plugin
    • Maven Exclude Dependency
    • Maven Life Cycle
    • Maven Repository
    • Maven POM File
    • Maven Plugins
    • What is Maven Plugins
    • Maven Interview Questions
    • Maven Flags
    • Maven Project
    • Maven Settings.XML
Home Software Development Software Development Tutorials Maven Tutorial Maven Run Single Test

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.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,130 ratings)

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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

This is a guide to Maven Run Single Test. Here we discuss the introduction, and how to run a single test in maven. project and test class. You may also have a look at the following articles to learn more –

  1. Maven Build Command
  2. Maven WAR Plugin
  3. Maven Profile
  4. Maven Versions
Popular Course in this category
Maven Training (4 Courses, 6 Projects)
  4 Online Courses |  6 Hands-on Project |  26+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Special Offer - Maven Training (4 Courses, 6 Projects) Learn More