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

As we have done throughout this chapter, we isolated all external invocations and focused only on the unit (play) that we're working on. Keep in mind that this isolation is limited only to the public and protected methods. When it comes to the actual implementation, we might choose to add the saveMove invocation to the play public method or one of the private methods that we wrote as a result of the refactoring we did earlier.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.2.240