Skip to content

Game Test Using Test Doubles

If You Want...

You can pull the latest code from the repository before starting, or continue with the code that you have.

Start With Passing Tests

Make sure all tests pass before getting started on this lab.

Goal

Now that Game is a pure domain class, we can more easily write tests for it.

A. Test Outcome

Create a test class named GameOutcomeTest.

Write tests that verify two of the game outcomes (player beats dealer and player goes bust) are evaluated properly by "stacking the deck", i.e., create a subclass implementation of Deck (no need to extract an interface) that provides a predictable set of cards.

Start with Two Tests

Only write two outcome tests. If you have time and have finished the rest of this lab, you can come back and write more.

When you're done, your tests will look something like:

@Test
public void playerBeatsDealer() {
  Deck stubDeck = new StubDeck(/* cards in a predictable order for this scenario */);
  Game game = new Game(stubDeck);  
  game.initialDeal();

  game.playerStands(); // make sure the player stands
  game.dealerTurn(); // dealer needs to take its turn

  String outcome = game.determineOutcome();
  assertThat(outcome)
    .isEqualTo(/* the expected outcome string */);
}

B. Solve Primitive Obsession

Now that we have test coverage of the determineOutcome method, we can fix the Primitive Obsession code smell of the String being returned by refactoring it to a new type.

Create a new enum class called GameOutcome, and refactor the tests and code to use this enum instead of the String.


You are done!