setenv CLASSPATH /home/uni1/jeffery/lab11:/home/uni1/jeffery/junit3.8.1/junit.jar-------------------------------------------------------------------------
setenv CLASSPATH .:/home/uni1/jeffery/junit3.8.1/junit.jar
Add a line at the beginning of the file:
import junit.framework.*;Change the class declaration to:
public class yourclass extends TestCase {
...
}
Then, write test methods. Those methods should have names starting with
"test".
The assertTrue() and assertEquals() are to be used in those methods.
For example:
public void testAdd() {
double result= fValue1 + fValue2;
assertTrue(result == 6);
}
public void testEquals() {
assertEquals(12, 12);
assertEquals(12L, 12L);
assertEquals(new Long(12), new Long(12));
assertEquals("Size", 12, 13);
assertEquals("Capacity", 12.0, 11.99, 0.0);
}
Then add a suite method if you want to use suite:
public static Test suite() {
return new TestSuite(SimpleTest.class);
}
Finally, add the following line to main() method:
junit.textui.TestRunner.run(suite()); #if you use suiteor,
junit.textui.TestRunner.run(which can run the Junit in the text mode if you excute the class with java..class); # if you don't use suite
This has a big advantage: you don't need to make any changes to the existing class files, so they aren't cluttered up by testing code. In your new test file, the following is still necessary:
import junit.framework.*;
public class yourclass_tester extends TestCase {
...
}
In the new class, you need to create the variables and instances of the
classes you want to test, and then write the test methods (the names must
start with "test"; JUnit automatically calls those methods), also create
suite method if needed.
Then add a main method:
public static void main(String args[]) {
junit.textui.TestRunner.run(yourclass_tester.class);
}
or,
public static void main(String args[]) {
junit.textui.TestRunner.run(suite());
}
if you use suite.You may refer to the /home/uni1/jeffery/junit3.8.1/junit/samples/money/MoneyTest.java file for more details.
import junit.framework.*;
/**
* TestSuite that runs all the sample tests
*
*/
public class AllTests {
public static void main (String[] args) {
junit.textui.TestRunner.run (suite());
}
public static Test suite ( ) {
TestSuite suite= new TestSuite("All JUnit Tests");
suite.addTest(VectorTest.suite());
suite.addTest(SimpleTest.suite());
suite.addTest(new TestSuite(junit.samples.money.MoneyTest.class));
suite.addTest(junit.tests.AllTests.suite());
return suite;
}
}