Refactoring


The method basicGradePointsFor in BasicGradingStrategy is now superfluous. You can inline it and eliminate the method basicGradePointsFor.

 package sis.studentinfo; public class BasicGradingStrategy implements GradingStrategy {    public int getGradePointsFor(Student.Grade grade) {       switch (grade) {          case A: return 4;          case B: return 3;          case C: return 2;          case D: return 1;          default: return 0;       }    } } 

Is there any more need for the separate subclass RegularGradingStrategy? There's not much left to it:

 package studentinfo; public class RegularGradingStrategy    extends BasicGradingStrategy { } 

You can eliminate this class by changing the gradeStrategy reference in Student to use BasicGradingStrategy as the default.

 public class Student {    ...    private GradingStrategy gradingStrategy =       new BasicGradingStrategy();    ... 

All the work you have done since learning about inheritance has been on the code side only. You have not changed any tests. (I hope you have been running your tests with each incremental change.) You have not changed any behavior; you have only changed the way in which the existing behavior is implemented. The tests in Student for GPA adequately cover the behavior.

However, the tests in Student cover behavior within the context of managing students. You want to additionally provide tests at the unit level. The general rule is to have at least one test class for each production class. You should test each of HonorsGradingStrategy and BasicGradingStrategy individually.

Here is a test for BasicGradingStrategy:

 package sis.studentinfo; import junit.framework.*; public class BasicGradingStrategyTest extends TestCase {    public void testGetGradePoints() {       BasicGradingStrategy strategy = new BasicGradingStrategy();       assertEquals(4, strategy.getGradePointsFor(Student.Grade.A));       assertEquals(3, strategy.getGradePointsFor(Student.Grade.B));       assertEquals(2, strategy.getGradePointsFor(Student.Grade.C));       assertEquals(1, strategy.getGradePointsFor(Student.Grade.D));       assertEquals(0, strategy.getGradePointsFor(Student.Grade.F));    } } 

A corresponding test for HonorsGradingStrategy:

 package sis.studentinfo; import junit.framework.*; public class HonorsGradingStrategyTest extends TestCase {    public void testGetGradePoints() {       GradingStrategy strategy = new HonorsGradingStrategy();       assertEquals(5, strategy.getGradePointsFor(Student.Grade.A));       assertEquals(4, strategy.getGradePointsFor(Student.Grade.B));       assertEquals(3, strategy.getGradePointsFor(Student.Grade.C));       assertEquals(2, strategy.getGradePointsFor(Student.Grade.D));       assertEquals(0, strategy.getGradePointsFor(Student.Grade.F));    } } 

Don't forget to add these tests to the suite defined in AllTests.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net