A Brief JUnit Introduction
CS 162 - Spring 2008 - Dr. Liffick

Goals
- to learn about a helpful testing tool you need to use; this is not an assignment

Overview
JUnit is a tool that helps you test your program. You can learn more about it at http://junit.sourceforge.net/ and especially at http://junit.sourceforge.net/doc/cookbook/cookbook.htm. It's incorporated into Eclipse.

Essentially, it helps you build automated test cases of input and expected output. It's a handy tool to easily run repeated tests on your code to uncover bugs. We're using JUnit 4.

Test Cases
In general, for any testing, you should create test cases that contain pairs of input and expected output. For example, with input of 15, 20, and 25, you would expect a mean of 20. You should think about your expected output before you run the program and say "well, that looks close" when you see the answer.

For instance, for the Statistician, I might create a list of three values as above, and write down what I expect the length, mean, sum, largest, and smallest to be. Then I'd write down another value (say -5) and write the answers I'd expect after that insert.

As in real life, bugs lurk in corners. They also are found at boundaries and edges. As you build test cases, check the limits. For example, for Statistician, there should be some negative numbers, zero, and some values with fractional parts.

Round numbers are easier to work with in hand-computing correct expected output. For example, a check of the mean is easy if all the numbers are the same. That shouldn't be the only check, but it could be one of them.

Using JUnit
JUnit lets you set up input, call the methods of your class, and compare the actual output to your expected output. The Statistician files include a JUnit test file.

Open Eclipse and the Statistician project. Click on StatTest.java and choose Run / Run As ... and JUnit.

When you run, the package explorer changes to the JUnit panel. Because the program you have does not pass the tests, you see a red bar and red Xs in front of the cases. There were 15 runs, 2 errors, and 13 failures. Let's examine one.

Double-click on the testConstructor case and it is selected in the code at right. At lower left, the failure trace states that the error is "Mean wrong". In the code, you see that "Mean wrong" is the message from the third call to assertTrue. The first two calls worked because the sum and length functions are returning 0. But mean, largest, and smallest are not returning Double.NaN as specified. Fix that in the Statistician code and run the test again. Now that test is passed. That doesn't mean that your code is "right". But it doesn't have the bug of returning 0 for mean, largest, and smallest when there are no values.

Your completed Statistician class must get through the tests in StatTest with no errors or failures.

Building a JUnit test.
From the File menu, choose New and JUnit Test Case (near the bottom). Fill in the fields reasonably including Statistician for the class under test. I used StatTestToo for the name. Be sure you click on New JUnit 4 test at the top of the panel. Click finish.

In the file that's created, import the needed supporting classes:

   import static org.junit.Assert.*;
   import org.junit.Test;

Create a test case. Create a function inside the class that declares and creates a Statistician, and inserts the value 42 into it. The assertTrue function checks whether its second parameter is true. If it isn't, the reason in the first parameter is printed.
   @Test
   public void few( ) {
      Statistician s = new Statistician( );
      s.insert(42);
      assertTrue("Length should be one", s.length( ) == 1);
   }
Run that test class. (You may need to get back to the Package Browser in Eclipse to find it.) Running it shows red (and an X before the case name). Modify the Statistician to set count to zero to start, increment count on insert (for now you can do only that as long as you come back and fix it), and return count from the length method. Run the test again. You should see green (and a check mark before the case name). Insert some more values, think about the expected output, and create another assertion. Does that work? Probably not. That's okay for now.

Look at the addException case in StatTest for an example of how to test that an exception is when expected. It looks like this:

   @Test(expected=NullPointerException.class)
   public void addException( ) {
      Statistician a = new Statistician( );
      Statistician b = null;	
      a.add(b);		
   }

JUnit can be a valuable tool for a developer, but for now, you don't need to write your own JUnit tests. Instead, use the JUnit tests provided with the assignments. Develop a correct Statistician. The tests in StatTest will uncover a wide variety of bugs.