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