how-to-run-junit
how to run JUnit
Many IDEs have JUnit built-in (e.g. eclipse and BlueJ).
If yours doesn't, you can run JUnit tests via the command line as follows:
-
Download the two necessary
jar files
Store them in a fairly-general place within your Java development/coding directories
(intended to be shared by all your Java projects).
I use /Users/username/dev/ (where /Users/ is specific to MacOS,
and dev is a directory I made myself).
-
Either set your classpath to explicitly include the jars
(e.g. setenv CLASSPATH .::~/Src/junit-4.12.jar:hamcrest-core-1.3.jar),
or pass that to java/javac (e.g.
javac -classpath .::/Users/username/dev/junit-4.12.jar:/Users/username/dev/hamcrest-core-1.3.jar MyFile.java).
-
Compile your classes with javac.
-
Test classes don't have a main, so we trigger the tests by
running org.junit.runner.JUnitCore's main, passing it the name(s)
of what test-classes to run:
java org.junit.runner.JUnitCore MyFile.
(Of course, as mentioned above:
you'll need to include the classpath either via the environment-variable, or runtime switch.)
-
I like alias junit java -classpath .::~/Src/junit-4.12.jar:hamcrest-core-1.3.jar org.junit.runner.JUnitCore
-
Alternately,
I imagine
you can also streamline running the test by adding to the file MyClassTest
public static void main(String... __) { org.junit.runner.JUnitCore.main("MyClassTest"); }.