Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a specific test within junit

Tags:

junit

maven

We have several junit test cases in a particular file, how do I run a certain test alone.

Note: I am aware of the @Ignore annotation, but don't want to use it. Since you will still have to mark the rest of the use cases with the @Ignore option.

like image 233
priya Avatar asked Sep 07 '25 09:09

priya


2 Answers

You can specify the test you would like to run via the -Dtest= parameter. Just run it with this parameter values:

-Dtest=TestClass#singleTestCaseMethod

It is only supported in JUnit 4.x. More information: Running a single test

like image 120
guerda Avatar answered Sep 09 '25 21:09

guerda


This depends on how you're running the tests.

If you're using Eclipse, you can either select the name of the method in the Java Editor and press F11/ctrl-F11 to Run As JUnit. This also works if the cursor is on the method name itself.

If you've already run the tests, and the JUnit view is open in Eclipse, you can rerun a particular method by right clicking on the test and selecting Run/Debug. There will be similar functionality in Intellij.

enter image description here

If you're running from the command line, you can use JUnitCore.

java org.junit.runner.JUnitCore TestClass1 TestClass2

EDIT: For maven, you can specify the name of the method on the command line:

mvn -Dtest=TestCircle#mytest test

See the documentation for Maven Surefire - Running a Single Test.

like image 35
Matthew Farwell Avatar answered Sep 09 '25 19:09

Matthew Farwell