Computer Science Department, Bucknell University

CSCI 479 JUnit - Unit Testing in Java
Sample Program to Demo JUnit
Fall 2009
by Professor Dan Hyde


  1. These examples use JUnit 4.5 which requires Java 1.5 or later version.

  2. These examples do NOT use the Eclipse IDE but the Unix command line.

  3. Add the following two lines to your ~/.cshrc file on Linux if you don't have them in the ~/.cshrc file. You can use any text editor such as emacs to edit the ~/.cshrc file.
    ### CLASSPATH - environment variable for JAVA
    setenv CLASSPATH .:/home/accounts/COURSES/cs475/common-files/junit-4.5.jar:$CLASSPATH
    
    Make sure the CLASSPATH variable in your .cshrc file is correct. If you have two CLASSPATH lines, you must remove the old one or combine them.

    Now type

     
    source ~/.cshrc
    
    and if no errors, it should display nothing.

  4. Create a new directory and copy and paste the following and name it SimpleTest.java
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import static org.junit.Assert.assertEquals;
    import junit.framework.JUnit4TestAdapter;
    
    /**
     * Some simple tests.
     *
     */
    public class SimpleTest {
    
        protected int fValue1;
        protected int fValue2;
    
        @Before public void setUp() {
    
    	fValue1= 2;
    	fValue2= 3;
        }
    
        @Test public void testAdd() {
    
    	double result= fValue1 + fValue2;
    	// forced failure result == 5
    	boolean check = (result == 6);
    	assertEquals("5 and 6 are not the same", false, check);
        }
    
        @Test(expected = java.lang.ArithmeticException.class)
    	public void testDivideByZero() {
    
    	int zero= 0;
    	int result= 8/zero;
        }
    
        @Test public void testEquals() {
    
    	assertEquals("12 equals 12", true, (12 == 12));
    	assertEquals("12L equals 12L", true, (12L == 12L));
    	Long l1 = new Long(12);
    	Long l2 = new Long(10);
    	boolean check = l1.equals(l2);
    	assertEquals("Long(12) doesn't equal Long(10)", 
    		     false, check);
    
        }
    
        // Need to call the JUnit runner
        public static junit.framework.Test suite() {
    	return new JUnit4TestAdapter(SimpleTest.class);
        }
    }
    
  5. Compile the program
    javac SimpleTest.java
    

  6. Run the JUnit 4 runner.
    java org.junit.runner.JUnitCore SimpleTest
    
    Use the -ea option if you have asserts.
    java -ea org.junit.runner.JUnitCore SimpleTest
    

  7. Now change the assertion on line 27 from false to true, i.e., before change
    assertEquals("5 and 6 are not the same", false, check);
    
    after change
    assertEquals("5 and 6 are not the same", true, check);
    

    Observe what happens.

  8. Copy the Latitude.java and TestLatitude.java from the Linux file system
    cp ~cs475/public_html/2008-2009/code/junit/latitude/*.java .
    

  9. Compile and run the JUnit test for the Latitude class and observe what happens.

  10. Copy the Money class and its associated interface from the Linux file system
    cp -r ~cs475/public_html/2008-2009/code/junit/money .
    

  11. Read the Money class and develop TestMoney class (TestMoney.java) that tests the following cases using JUnit.
    1. Test if two Money are equal then they are created the same.
    2. Test if two Money are different when they are created differently.
    3. Test if one Money added to another results the correct amount.
    4. Test if a Money is added by zero remains the same.
  12. Submit the TestMoney.java and a record of running the program using either copy-and-paste or
    script
    in electronic form and in printing form.


Page maintained by Dan Hyde, hyde at bucknell.edu Last update October 28, 2009
Back to CSCI 479's home page.