Lab 18: Unit Testing Exercise
Objective
To get experience using junit to create and run unit tests.
Task
For this assignment, you will be adding JUnit unit tests to this Student class. This way, the methods of the class can be tested automatically.
Details
- Download the Student.java file.
- Add a test for the
addCoursemethod. Your test should create three student objects and give them various classes by callingaddCourse. For each one, you should useassertEqualsto verify that their GPA and credits are correct. For the GPA, pass a delta value intoassertEquals, since floating-point comparisons may not be exact. Be sure to include the edge case where a student fails a course, making sure it's not added to their credits. - Add a test for
canGraduate. You should create four students and give them a GPA and credits to test each of the four possible states: (neither sufficient, sufficient GPA but not credits, sufficient credits but not GPA, both sufficient). UseassertEqualsto make sure each has the right outcome. - Add a test for
getYear. Make four different students with each level of credits and useassertEqualsto verify that they are given the correct year.
Make sure that you run the tests. As the class is, all of your tests should all pass.
Verify that introducing a bug into the Student class causes your tests to fail. For example,
if you comment out the line in addCourse which computes the GPA, or otherwise
change the code such that its answers are not right, your tests should begin to fail.
Submitting
For this lab, you should submit the StudentTest.java file with your unit testing for the Student class.