Computer Science Department, Bucknell University

CSCI 475 JUnit - Unit Testing in Java
JUnit 4.0 with Eclispe
Fall 2006
Professor Dan Hyde


This example uses JUnit 4.0 which requires Java 1.5. We assume Eclipse 3.2 on Linux machines.
  1. Run Eclipse.

  2. Create a new Java project and call it "Latitude" and then click on Latitude to select it. Import the Latitude.java file from sample program in previous step. Do this by selecting File->Import->General->File System then pressing Next. Use the top Browse to set the directory. Then click on the file and press Finish.

  3. Click on Latitude project to select it. Select File->JUnit Test Case. When the dialog box opens select "New JUnit 4 Test" (near the top). Type in the name LatitudeTest and click on "Generate Comments." Don't click Finish yet!

    Look for a warning that the compiler must be 5.0 compliant. Click on the link and change the compliance level to 5.0.

    You should be back to the previous dialog box. Look for a warning that says JUnit 4 is not on the build path. Click on the "Click Here." This will should remove the warning and add JUnit 4 under Latitude project.

  4. In front of the "public class LatitudeTest" line copy and paste the following imports:
    import org.junit.Test;   // need to import for JUnit 4.0
    import static org.junit.Assert.assertEquals;
    import org.junit.After;   // need if use @After
    import org.junit.Before;  // need if use @Before
    import org.junit.Ignore;  // need if use @Ignore
    
    One would think Eclipse would add these for you. (?)

    Note that Eclipse flags with a litle light bulb a warning that org.junit.After and orig.junit.Ignore are not used in this example.

  5. Copy and paste the following code inside the class.
      private Latitude N0;
      private Latitude N90;
    
      // Done before any tests.  Method name can be anything.
      @Before public void setUp1() { 
    
        N0 = new Latitude();
        N90 = new Latitude(90.0, 0.0, 0.0, "N");
      }
    
      // Each method with tests needs @Test
      @Test public void testLatitudeConst() {
    
        // O degrees N from default constructor should equal 
        // 0 degrees N from 4 arg constructor
        Latitude N0expected = new Latitude(0.0, 0.0, 0.0, "N");
        // check if true with assert(); 
        // Must have -ea option on java command or asserts are ignored.
        assert(N0.equals( N0expected));
        // 0 degrees N should be equal to 0 degrees S.
        Latitude S0expected = new Latitude(0.0, 0.0, 0.0, "S");
        // check if equals with assertEquals()
        assertEquals(N0, S0expected);
      }
    
  6. Select Run->Run as->JUnit Test and press "OK." On left you should see the JUnit Report and the infamous green bar.

  7. Change the "N0" in the assert() to "N90" and run again. You should see the error reported and a red bar.

    Look below the JUnit report and find the Failure Trace. It will show the line in the source where the error was detected. If you double-click on this line, it will highlight that line in the source.


Page maintained by Dan Hyde, hyde at bucknell.edu Last update October 3, 2006
Back to CSCI 475's home page.