// ------------------------------------------------------------------------- /** * This test class shows how one might specify required behavior in an * assignment using a series of test cases. * * @author Stephen Edwards * @version Feb 22, 2005 */ public class AssignmentSpecificationTests extends junit.framework.TestCase { //~ Instance/static variables ............................................. private Student fred; private Student wilma; private Gradebook gradebook; //~ Methods ............................................................... // ---------------------------------------------------------- /** * Sets up the test fixture. * Called automatically before every test case method. */ protected void setUp() { fred = new Student( "Fred" ); wilma = new Student( "Wilma" ); gradebook = new Gradebook(); } // ---------------------------------------------------------- /** * Test the {@link Student#name()} accessor. */ public void testName1() { assertEquals( "Fred", fred.name() ); assertEquals( "Wilma", wilma.name() ); } // ---------------------------------------------------------- /** * Test the {@link Student#setName(String)} setter method. */ public void testSetName() { fred.setName( "XYZ" ); assertEquals( "XYZ", fred.name() ); fred.setName( null ); assertNull( fred.name() ); } // ---------------------------------------------------------- /** * Test computing the assignment average when there are no assignments. */ public void testNoAssignmentAverage() { assertEquals( 0, fred.assignmentAverage() ); } // ---------------------------------------------------------- /** * Test computing the assignment average when there are some assignments. */ public void testAssignmentAverage() { fred.addAssignment( 70 ); assertEquals( 70, fred.assignmentAverage() ); fred.addAssignment( 80 ); assertEquals( 75, fred.assignmentAverage() ); fred.addAssignment( 60 ); assertEquals( 70, fred.assignmentAverage() ); wilma.addAssignment( 60 ); assertEquals( 60, wilma.assignmentAverage() ); wilma.addAssignment( 65 ); assertEquals( 62, wilma.assignmentAverage() ); } // ---------------------------------------------------------- /** * Test the {@link Student#toString()} method. */ public void testToString() { fred.addAssignment( 70 ); assertEquals( "Fred (70)", fred.toString() ); } // ---------------------------------------------------------- /** * Try {@link Gradebook#findStudent(String)} on an empty gradebook. */ public void testSearchOfEmptyBook() { assertNull( gradebook.findStudent( "" ) ); } // ---------------------------------------------------------- /** * Try {@link Gradebook#findStudent(String)} on a gradebook containing * a single student. */ public void testFindFred() { gradebook.addStudent( fred ); assertSame( fred, gradebook.findStudent( "Fred" ) ); assertSame( fred, gradebook.findStudent( fred.name() ) ); } // ---------------------------------------------------------- /** * Try {@link Gradebook#findStudent(String)} on a gradebook containing * multiple students. */ public void testFindFredAndWilma() { gradebook.addStudent( fred ); gradebook.addStudent( wilma ); assertSame( fred, gradebook.findStudent( "Fred" ) ); assertSame( wilma, gradebook.findStudent( "Wilma" ) ); assertNull( gradebook.findStudent( "Frank" ) ); } }