Run single test in maven

In this post, we will see how to run single test in maven.

If you want to execute all the Junit testcases in maven. You can run it below command.

Run all testcase

mvn test

You need to go to the project location which contains pom.xml and execute above command.

Run all testcases from a test class

You can use -Dtest=$JunitClassName to execute all the test cases in particular Junit class.
For example:
Let’s say you have below test class AssertSameTest.

Here we have used assertSame() method of Assertions class to see if two strings refer to same object.
You want to run only AssertSameTest in maven.

You can simply use below command.

mvn test -Dtest=AssertSameTest

Run specific test method from a test class

Let’s say you want to execute only special test case in AssertSameTest class, you can run as below.

mvn test -Dtest=AssertSameTest#test3

Above command will execute test3 testcase of AssertSameTest class.

Run multiple test methods from a test class

mvn test -Dtest=AssertSameTest#test1+test2

Above command will execute test1 and test2 test method of AssertSameTest class.

You can also use pattern matching to execute the testcases.

mvn test -Dtest=AssertSameTest#test*

It will run all the testcases which starts from test in AssertSameTest.

That’s all about how to run single JUnit test in maven.

Related Posts

  • 28 March

    Generate source code jar for Maven based project

    Table of ContentsDeploy the jarCheck the local repository In this tutorial, we will see how to generate source code jar for maven based project. If you have simple java project, then you need to convert it maven project. Sometimes, you need to pack source code with your project jar. This is extremely useful when people […]

  • 21 March

    Install Maven on Mac

    Table of ContentsInstall maven manuallyDownload and extract MavenUpdate M2_HOME and PATHVerfiy maven installationInstall maven using Homebrew In this post, we will see how to install maven on Mac. If you are using the latest MacOSX, maven may be built-in and can be found at /usr/share/maven. Execute below command on terminal to see if you have […]

  • 21 March

    Skip tests in maven

    Table of ContentsUsing maven.test.skip=trueUsing SkipTestsSkip tests in Maven in eclipseConclusion In this tutorial, we will see how to skip tests in maven. It is not a good idea to skip junit tests while building project in maven but sometimes, you may need to skip it for building new code and want to test it quickly. […]

  • 23 August

    log4j properties file example

    Logging is essential part of programming. It helps developer to track code workflow and fix bugs efficiently. If we get any issue in the code, we check logs for that workflow or functionality. Log4j is fast , reliable logging framework and can be easily integrated with the code. It is possible to enable logging at […]

  • 23 May

    log4j xml configuration example

    Logging is essential part of programming. It helps developer to track code workflow and fix bugs efficiently. If we get any issue in the code, we check logs for that workflow or functionality. Log4j is fast , reliable logging framework and can be easily integrated with the code. It is possible to enable logging at […]

  • 20 February

    Log4j Maven configuration example

    Logging is essential part of programming. It helps developer to track code workflow and fix bugs efficiently. If we get any issue in the code, we check logs for that workflow or functionality. Log4j is fast , reliable logging framework and can be easily integrated with the code. It is possible to enable logging at […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.