JUnit is one of the most popular unit testing frameworks for Java so all IDEs and build tools have great support for Unit, including Eclipse and NetBeans. Creating and running JUnit tests in Eclipse is quick and easy, as described in JUnit Tutorial for beginner with Eclipse, and sometimes you also need to compile and run unit tests from command line, e.g. for quick testing regression of a change without opening the IDE.

 

1. Compile JUnit tests in command line

To compile test classes, the JUnit JAR file must be present in the classpath. Use the -cp option with javac command to specify JUnit JAR file:

javac -cp <junit-jar-file>;. TestClass1.java TestClass2.java
Note that in this command, the classpath must include the dot to denote the current directory that contains Java source files. And the test classes are separated by spaces.

For example, the following command compiles 2 test classes UserDAOTest.java and ProductDAOTest.java with JUnit 4:

javac -cp junit-4.12.jar;. UserDAOTest.java ProductDAOTest.java
Here, the JUnit JAR file junit-4.12.jar is in the same directory as the test classes. You can download JUnit JAR file here.

 

2. Run JUnit tests in command line

To run the test classes in command line, you have to specify JUnit JAR file and Hamcrest library JAR file (JUnit depends on it) in the classpath. You can download Hamcrest JAR file here. In the options for java command, specify the main class is org.junit.runner.JUnitCore followed by the names of the test classes (without .java extension). Here’s the syntax:

java -cp <junit-jar>;<hamcrest-jar>;. org.junit.runner.JUnitCore  TestClass1 TestClass2
For example, the following command runs two test classes UserDAOTest and ProductDAOTest:

java -cp junit-4.12.jar;hamcrest-core-1.3.jar;. org.junit.runner.JUnitCore UserDAOTest ProductDAOTest


The output would looks like this (a Command Prompt on Windows) if the tests succeeded:

test successful

or if there’s a test failed:

test failed

 

3. Run JUnit tests by Maven in command line

If your Java project is Maven-based, you can execute tests in the command prompt by specifying the goal testwhen running Maven, for example:

mvn test
mvn clean test
mvn clean compile test
And make sure that you have JUnit dependency in the pom.xml file:

<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.12</version>
	<scope>test</scope>
</dependency>
 

Video:

 

References:

 

Other JUnit Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment