Answer 3: Adding a Fixture

import junit.framework.TestCase;

public class FractionTest extends TestCase {
    
    private Fraction half;
    private Fraction fourth;

    protected void setUp() {
        half = new Fraction(2, 4);
        fourth = new Fraction(1, 4);       
    }
    
    public void testAddition() {
        Fraction actual = half.add(fourth);
        Fraction expected = new Fraction(3, 4);
        assertEquals(expected, actual);
    }
    
    public void testSubtraction() {
        Fraction actual = half.subtract(fourth);
        Fraction expected = fourth;
        assertEquals(expected, actual);
    }
    
    public void testAddNumberToItself() {
        Fraction actual = half.add(half);
        Fraction expected = new Fraction(1, 1);
        assertEquals(expected, actual);
    }
    
}

Previous | Next | Top | Cafe con Leche

Copyright 2005-2007 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified October 4, 2006