Testing Happens at Multiple Levels

Different Perspectives

JUnit - Sample Test Class

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class PointTest
{
    private Point testPoint;

    @Before
    public void setUp() throws Exception
    {
        testPoint = new Point(5.0, 6.0);
    }

    @Test
    public void testToString()
    {
        assertEquals("(5.0, 6.0)", testPoint.toString());
    }
}

Testing Example

public class Estimator
{
    public static int totalCost(boolean fast, boolean good)
    {
        int total = 0;
        if (fast)
        {
            total += 5;
        }
        if (good)
        {
            total += 10;
        }
        return total;
    }
}

Method Coverage

    @Test
    public void testTotalCostSlowBad() {
        assertEquals(0, Estimator.totalCost(false, false));
    }

Style Suggestion

    @Test
    public void testTotalCostSlowBad() {
        assertEquals("Estimate should have been 0.",
                     0, Estimator.totalCost(false, false));
    }

Statement Coverage

    @Test
    public void testTotalCostFastGood() {
        assertEquals(15, Estimator.totalCost(true, true));
    }

Branch Coverage

    @Test
    public void testTotalCostSlowBad() {
        assertEquals(0, Estimator.totalCost(false, false));
    }

    @Test
    public void testTotalCostFastGood() {
        assertEquals(15, Estimator.totalCost(true, true));
    }

Path Coverage

    @Test
    public void testTotalCostSlowBad() {
        assertEquals(0, Estimator.totalCost(false, false));
    }
    @Test
    public void testTotalCostFastGood() {
        assertEquals(15, Estimator.totalCost(true, true));
    }
    @Test
    public void testTotalCostSlowGood() {
        assertEquals(10, Estimator.totalCost(false, true));
    }
    @Test
    public void testTotalCostFastBad() {
        assertEquals(5, Estimator.totalCost(true, false));
    }

Path Coverage

Test-Driven Development

Developing Test Cases

Regression Testing

Brainstorm Some Tests...

/**
 * Returns the point with the smallest x-coordinate 
 * among all points in the array. In the case of a tie, 
 * the point that appears first will be returned.
 * 
 * @param points - An array of point objects
 * @return - The leftmost point
 * @throws - IllegalArgumentException If the length 
 *                                  of the array is 0.
 * @throws - NullPointerException If the array, or any 
 *                              entries in the array,
 *                              are null.
 */

public static Point findLeftmost(Point[] points) 
          throws IllegalArgumentException,
                 NullPointerException

    
Input Expected Result
Two points, leftmost is first:
[(1.0, 1.0), (2.0, 1.0)]
(1.0, 1.0)