Refactor Primitive Obsession¶
Do This First...
Make sure all tests pass before getting started on this lab.
Characterization Test¶
Before you start, add the following Characterization test to ensure you don't break the way Suit is displayed on a card:
@Test
public void cardDisplaysSuitAsSymbol() throws Exception {
Card spadesCard = new Card("♠", "9");
assertThat(spadesCard.display())
.contains("│ ♠ │");
}
Copy and paste the above test into the CardTest
class and then run it. It should pass.
Extract Suit to Enum¶
-
Inside the
Card
class, replace theSuit
instance variable (field) with anEnum
.- This includes
Card
's constructor.
- This includes
-
Refactor the entire codebase so all usages are of the new
Suit
type instead of theString
primitive.Note
Don't refactor the
equals()
,hashCode()
, andtoString()
methods as these were code-generated, simply delete them and use your IDE to regenerate them. -
Be sure to fix any tests that might need to change as a result of this refactoring.
Run All Tests...
Make sure all the tests pass when you are done.
You are done!