By KK.

2010-02-10 08:07:32 8 Comments

I would like to run JUnit test cases from the command line. How can I do this?


@lzap 2011-08-24 08:37:59

For JUnit 5.x it's:

java -jar junit-platform-console-standalone-<version>.jar <Options>

Find a brief summary at and full details at

For JUnit 4.X it's really:

java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]

But if you are using JUnit 3.X note the class name is different:

java -cp .:/usr/share/java/junit.jar junit.textui.TestRunner [test class name]

You might need to add more JARs or directories with your class files to the classpath and separate that with semicolons (Windows) or colons (UNIX/Linux). It depends on your environment.

Edit: I've added current directory as an example. Depends on your environment and how you build your application (can be bin/ or build/ or even my_application.jar etc). Note Java 6+ does support globs in classpath, you can do:

java -cp lib/*.jar:/usr/share/java/junit.jar ...

Hope it helps. Write tests! :-)

@Goaler444 2013-11-10 11:33:41

@Izap Any idea to programatically determine whether a test is using JUnit4 or JUnit3?

@lzap 2013-11-11 08:44:33

Class.forName I guess. It's been years I was programming in Java for the last time...

@n611x007 2014-08-13 10:04:41

and what if you are using android?

@lzap 2015-03-04 12:15:38

Then try the first command, if it does not work the latter. Or read Android docs... dunno.

@NamshubWriter 2015-04-18 18:37:12

If the version of your JUnit jar is at least 4.0, then you should be able to use org.junit.runner.JUnitCore as your main class, that that will work whether your tests are written in JUnit3-style or JUnit4-style.

@David Tonhofer 2015-08-08 16:54:03

Documentation for the "-cp" argument (i.e. the CLASSPATH) is here (Java 7, Unix) and here (Tutorial) and here (Java 8, Unix) and here (Java 8, Windows). Apparently wildcards in the classpath are now supported.

@Fla 2017-04-06 07:49:26

That works well, however I'd like to give another argument to the test (the url it should go to) how can I do that? How can I get it inside the Java code?

@Philip Rego 2018-12-10 19:09:51

Downvote. For JUnit 4.x it's not right. Your instructions give "Could not find class: [test class name]" Even when [test class name] is in the classpath.

@Yuci 2016-10-08 12:24:06

Maven way

If you use Maven, you can run the following command to run all your test cases:

mvn clean test

Or you can run a particular test as below

mvn clean test -Dtest=your.package.TestClassName
mvn clean test -Dtest=your.package.TestClassName#particularMethod

If you would like to see the stack trace (if any) in the console instead of report files in the target\surefire-reports folder, set the user property surefire.useFile to false. For example:

mvn clean test -Dtest=your.package.TestClassName -Dsurefire.useFile=false

Gradle way

If you use Gradle, you can run the following command to run all your test cases:

gradle test

Or you can run a particular test as below

gradle test --tests your.package.TestClassName
gradle test --tests your.package.TestClassName.particularMethod

If you would like more information, you can consider options such as --stacktrace, or --info, or --debug.

For example, when you run Gradle with the info logging level --info, it will show you the result of each test while they are running. If there is any exception, it will show you the stack trace, pointing out what the problem is.

gradle test --info

If you would like to see the overall test results, you can open the report in the browser, for example (Open it using Google Chrome in Ubuntu):

google-chrome build/reports/tests/index.html

Ant way

Once you set up your Ant build file build.xml, you can run your JUnit test cases from the command line as below:

ant -f build.xml <Your JUnit test target name>

You can follow the link below to read more about how to configure JUnit tests in the Ant build file:

Normal way

If you do not use Maven, or Gradle or Ant, you can follow the following way:

First of all, you need to compile your test cases. For example (in Linux):

javac -d /absolute/path/for/compiled/classes -cp /absolute/path/to/junit-4.12.jar /absolute/path/to/

Then run your test cases. For example:

java -cp /absolute/path/for/compiled/classes:/absolute/path/to/junit-4.12.jar:/absolute/path/to/hamcrest-core-1.3.jar org.junit.runner.JUnitCore your.package.TestClassName

@Josie Thompson 2018-01-11 02:20:12

I like that this answer has examples for multiple technologies, kudos!

@midori 2018-04-16 21:34:51

what about groovy tests using the last approach?

@Eric Duminil 2019-10-08 13:23:48

Well deserved golden badge ;)

@djangofan 2013-04-15 18:06:06

Personally I would use the Maven surefire JUnit runner to do that.

@Stefan van den Akker 2015-06-13 13:40:16

With JUnit 4.12 the following didn't work for me:

java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]

Apparently, from JUnit 4.11 onwards you should also include hamcrest-core.jar in your classpath:

java -cp .:/usr/share/java/junit.jar:/usr/share/java/hamcrest-core.jar org.junit.runner.JUnitCore [test class name]

@Vladimir Nazarenko 2016-07-30 09:11:35

Had the same issue with JUnit 4.12. Came up with a similar solution, but it didn't work for me, failing to load JUnitCore. I basically switched to JUnit 4.8.2 as it does not require to include hamcrest-core.jar in the classpath.

@rayryeng - Reinstate Monica 2017-03-16 04:53:03

Confirmed that this must be done with JUnit 4.12. +1.

@Raffi Khatchadourian 2017-05-16 18:50:21

This worked for me: java -cp .:/usr/share/java/junit4.jar org.junit.runner.JUnitCore [test class name]

@Raghu K Nair 2015-02-17 20:30:12

Alternatively you can use the following methods in JunitCore class

run (with Request , Class classes and Runner) or runClasses from your java file.

@user1092126 2014-12-01 11:18:34

If your project is Maven-based you can run all test-methods from test-class CustomTest which belongs to module 'my-module' using next command:

mvn clean test -pl :my-module -Dtest=CustomTest

Or run only 1 test-method myMethod from test-class CustomTest using next command:

mvn clean test -pl :my-module -Dtest=CustomTest#myMethod

For this ability you need Maven Surefire Plugin v.2.7.3+ and Junit 4. More details is here:

@rogue lad 2014-05-15 07:27:20

In windows it is

java -cp .;/path/junit.jar org.junit.runner.JUnitCore TestClass [test class name without .class extension]

for example: c:\>java -cp .;f:/libraries/junit-4.8.2 org.junit.runner.JUnitCore TestSample1 TestSample2 ... and so on, if one has more than one test classes.

-cp stands for class path and the dot (.) represents the existing classpath while semi colon (;) appends the additional given jar to the classpath , as in above example junit-4.8.2 is now available in classpath to execute JUnitCore class that here we have used to execute our test classes.

Above command line statement helps you to execute junit (version 4+) tests from command prompt(i-e MSDos).

Note: JUnitCore is a facade to execute junit tests, this facade is included in 4+ versions of junit.

@Mohit Jain 2014-05-15 07:50:45

Please explain your answer in very brief.

@Mohit Jain 2014-05-16 16:22:16

I did not ask you to keep your answer brief. I requested to add some explanation (at least a brief explanation). It is a good practice to explain how your answer work. Readers may understand it, like it, upvote it.

@Ungeheuer 2016-11-01 19:57:35

so if I had a supplemental testing jar AND the vanilla junit jar, Id have to have both of those in java -cp command for anything to actually work? Is there a way around having to put all this into a command line so that I don't have to type as much stuff?

@Indraneel 2013-12-05 13:16:26

Actually you can also make the Junit test a runnable Jar and call the runnable jar as java -jar

@Indraneel 2014-12-05 05:37:16

In Eclipse , right click your JUnit project -> Click on Export --> Choose Java-> Runnable Jar File

@rand_acs 2012-08-15 08:02:12

The answer that @lzap gave is a good solution. However, I would like to add that you should add . to the class path, so that your current directory is not left out, resulting in your own classes to be left out. This has happened to me on some platforms. So an updated version for JUnit 4.x would be:

java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]

@panny 2013-02-07 01:30:26

is that supposed to be a semi colon?

@rand_acs 2013-02-07 07:44:24

@panny it's a semicolon on Windows. On n *nix environment (at least OSX and all the Linux distros I've used) you use a colon.

@Goaler444 2013-11-10 12:08:19

@rand_acs does the test class name need to be the fully classified class name ?

@rand_acs 2013-11-14 07:09:58

@Goaler444 Yes, I always use the full name, with all the namespaces specified.

@Gishu 2010-02-10 08:16:58

Ensure that JUnit.jar is in your classpath, then invoke the command line runner from the console

java org.junit.runner.JUnitCore [test class name]

Reference: junit FAQ

@Thilo 2010-02-10 08:20:34

you also need to set up the rest of your project's classpath.

@Philip Rego 2018-12-10 19:10:38

This just gives "Could not find class: [test class name]" even when [test class name] is in the classpath.

@cherouvim 2010-02-10 08:10:18

If you project is ant based then you should be able to do something like this from the console:

ant test

If this doesn't work, but still your project is ant based, you can run ant -p to list the main targets of the project.

@accuya 2013-06-12 13:47:26

the Q has nothing to do with ant

@cherouvim 2013-06-12 19:32:02

That's why I said "if your project is ant based". Note also that the OP may don't know about ant.

@Andrzej Rehmann 2013-11-26 16:03:27

I had this dillema, unit test files were not in folder marked as source by eclipse. That project uses ant to build, this was the proper way to run those JUnit tests.

Related Questions

Sponsored Content

47 Answered Questions

[SOLVED] How do I import an SQL file using the command line in MySQL?

14 Answered Questions

[SOLVED] How do I set a variable to the output of a command in Bash?

17 Answered Questions

[SOLVED] How do I run two commands in one line in Windows CMD?

51 Answered Questions

24 Answered Questions

[SOLVED] Is there an equivalent of 'which' on the Windows command line?

33 Answered Questions

[SOLVED] How do you assert that a certain exception is thrown in JUnit 4 tests?

15 Answered Questions

[SOLVED] How to get a list of user accounts using the command line in MySQL?

20 Answered Questions

[SOLVED] How to count lines in a document?

18 Answered Questions

[SOLVED] How to pass command line arguments to a rake task

Sponsored Content