CS 371 JUnit HowTo (by Yonggang Lu, edited by Clinton Jeffery)

  • add your working directory (with Java files to test) and /home/uni1/jeffery/junit3.8.1/junit.jar to your CLASSPATH in your .cshrc, for example:
    setenv CLASSPATH /home/uni1/jeffery/lab11:/home/uni1/jeffery/junit3.8.1/junit.jar
    
    -------------------------------------------------------------------------

    1. Before using Junit

    2. There are two ways to test a class file:

    (1). Use the same class file but inherit the TestCase class.

    First, make the following changes to your class file:

    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 suite
    
    or,
    junit.textui.TestRunner.run(.class); # if you don't use suite
    
    which can run the Junit in the text mode if you excute the class with java.

    (2). Use a different class file to test existing class files.

    This is very similar with (1) except that you write a different class file for testing purposes.

    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.

    3. The test suites can be run together in one file:

    The following example is from: .../junit3.8.1/junit/samples/AllTests.java
    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;
    	}
    	}
    

    4. Use the graphical interface:

    This can be done by typing "java junit.awtui.TestRunner" or "junit.swingui.TestRunner" on the command line. After a window appears, you can input a test class name to run the test.