O'Reilly logo

Test-Driven Java Development - Second Edition by Viktor Farcic, Alex Garcia

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required