home—lectures—recipe—exams—hws—D2L—breeze (snow day)
how to run JUnit
Many IDEs have JUnit built-in (e.g. eclipse and BlueJ).
However,
if you get a compile-time error about “class junit.framework.TestCase not found”,
it means that the following two steps haven't both been done:
(a) you have JUnit .jars downloaded, and
(b) your CLASSPATH includes it.
You can do these two thing from inside your IDE, or from the command-line
(which this page is all about).
Note:
It is not a requirement to use JUnit for our ITEC380 project.
You can strip out the mentions of the JUnit classes, and write your own:
void assertEquals( Object actual, Object expected ) {
if (!actual.equals(expected)) {
System.err.printf("assertion failed:\nExpect: %s\nActual: %s\n", expected.toString(), actual.toString() );
}
}
|
IDE-specific instructions
One student reports:
After downloading the two JUnit .jar files (below), I added them to the reference library for the
project that I added the U0 .jar file to originally.
Then I could right click and select Run As and select
JUnit Test.
I could run individual test methods/classes, or specify running all tests within the entire .jar file.
If somebody sends me (links to) good instructions for running/installing JUnit from inside
other IDEs, send me a link and I'll put it here.
Command-line
Installing JUnit
-
Download the two necessary
jar files
(junit and hamcrest-core).
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 test-classes with javac. (If they compile, you're good.)
Invoking JUnit tests yourself
JUnit test-classes don't have a main.
So instead, we can 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 MyTestClass |
We can simplify this in a couple ways:
-
I like
alias junit java --class-path :~/dev/junit-4.12.jar:~/dev/hamcrest-core-1.3.jar org.junit.runner.JUnitCore
-
Alternately, you can code the above method-call inside your own function:
public static void main(String... __) { org.junit.runner.JUnitCore.main("MyClassTest"); }.
(This may require having “.” in your CLASSPATH.)
-
(Related:)
Rather than calling JUnitCore.main, you could just call specfic test-methods yourself:
public static void main(String... args) {
ExprTest t = new ExprTest();
t.setUp(); t.testMethod1(); t.tearDown();
t.setUp(); t.testMethod2(); t.tearDown();
}
|
It's expected-protocol to call setUp/tearDown before/after calling each test-method.
(If your methods modify state, then this might be essential.)
home—lectures—recipe—exams—hws—D2L—breeze (snow day)
 This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland radford.edu |
 |