Skip to content

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

  1. Inside the Card class, replace the Suit instance variable (field) with an Enum.

    • This includes Card's constructor.
  2. Refactor the entire codebase so all usages are of the new Suit type instead of the String primitive.

    Note

    Don't refactor the equals(), hashCode(), and toString() methods as these were code-generated, simply delete them and use your IDE to regenerate them.

  3. 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!