I have installed JUnit on my Ubuntu 11.04
sudo apt-get install junit
Then created a test file like:
import org.junit.*;
public class TestBasicLinkedList {
@Test public void testCanAdd(String[] args) {
BasicLinkedList<Integer> list = new BasicLinkedList();
list.add(new BasicListNode<Integer>(1));
assertTrue(list.size() == 1);
assertTrue(list.getFirst().getElement().equals(1));
}
}
Then tried running in terminal
java org.junit.runner.JUnitCore TestBasicLinkedList.java
Got:
Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore
Caused by: java.lang.ClassNotFoundException: org.junit.runner.JUnitCore
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: org.junit.runner.JUnitCore. Program will exit.
How do I use JUnit? Is there a better shorter way of using it than the long command java org.junit.runner.JUnitCore TestBasicLinkedList.java
? Perhaps something like junit *.Tests.java
maybe?
To get you up and running, this is a basic test case:
The test class should extend TestCase, and test methods should start with "test", accept no arguments and return void.
To compile:
To run:
This is for JUnit 4; JUnit 3 uses different classes for executing tests (try junit.textui.TestRunner).
In general, JUnit (just like any Java library) needs to be on your classpath. I don't know where exactly Ubuntu stores junitX.jar, but you probably want something like this:
You'll need to compile
TestBasicLinkedList
first./usr/share/java/junit4.jar
is a symlink to the latest junit4 version./usr/share/java/junit.jar
is a symlink to the latest junit3 version.If your application requires that you to set
$JUNIT_HOME
:If your application requires it to have it in your
$CLASSPATH
:Just copy it into one of your application's classpath directories.