Specification – storing current move

Whenever we play a turn, it should be saved to the DB. The specification can be the following:

@Test 
public void whenPlayThenSaveMoveIsInvoked() {
  TicTacToeBean move = new TicTacToeBean(1, 1, 3, 'X');
  ticTacToe.play(move.getX(), move.getY());
  verify(collection).saveMove(move);
}

By now, you should be familiar with Mockito, but let us go through the code as a refresher:

  1.  First, we are instantiating a TicTacToeBean since it contains the data that our collections expect:
TicTacToeBean move = new TicTacToeBean(1, 1, 3, 'X'); 
  1.  Next, it is time to play an actual turn:
ticTacToe.play(move.getX(), move.getY()); 
  1.  Finally, we need to verify that the saveMove method is really invoked:
verify(collection, times(1)).saveMove(move); ...

Get Test-Driven Java Development - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.