JMU JMU - Department of Computer Science
Help Tools
Using JUnit with the CMD Shell


1 Configuration

Both the Java compiler and the Java interpreter need to be able to "find" the JUnit classes (which are contained in the file named junit.jar). You can either do this each time you use the compiler/interpreter or you can set an operating system environment variable named CLASSPATH.

2 Using the CLASSPATH Environment Variable

You can set the CLASSPATH as follows:
set CLASSPATH=.;directoryjunit.jar;%CLASSPATH%

where directory denotes the name of the directory/folder that contains the file junit.jar.

For example, assuming that junit.jar is in the current working directory, you can set the CLASSPATH as follows:

set CLASSPATH=.;junit.jar;%CLASSPATH%

After you have set the CLASSPATH you can compile your classes (including the test classes) and the usual way, and run a JUnit test as follows:

java org.junit.runner.JUnitCore ClassNameTest

where ClassName is the name of the class being tested. For example, to run AtomTest:

java org.junit.runner.JUnitCore AtomTest

3 Not Using the CLASSPATH Environment Variable

If you don't use set the CLASSPATH you must tell Java where to find JUnit every time you compile and/or run a test.

To compile:

javac -cp .;junit.jar ClassNameTest.java

where Name represents tha name of the class being tested (and can contain wildcards like *).

To run:

java -cp .;junit.jar org.junit.runner.JUnitCore ClassNameTest

where Name represents the name of the class being tested.

For example, to compile and run AtomTest:

javac -cp .;junit.jar AtomTest.java java -cp .;junit.jar org.junit.runner.JUnitCore AtomTest

4 Test Output

If all of the tests are successful, an "OK" message will be printed. If not, a long error message will be printed. Buried in the message will be information about where the errors occurred and what the errors were. To find them, look for the descriptions that you included in your calls to assertEquals().

Copyright 2019