Requirement 1 – store moves

We should be able to save each move to the DB. Since we already have all the game logic implemented, this should be trivial to do. Nonetheless, this will be a very good example of mock usage.

Implement an option to save a single move with the turn number, the x and y axis positions, and the player (X or O).

We should start by defining the Java bean that will represent our data storage schema. There's nothing special about it, so we'll skip this part with only one note.

Do not spend too much time defining specifications for Java boilerplate code. Our implementation of the bean contains overwritten equals and hashCode. Both are generated automatically by IDEA and do not provide a real value, except to satisfy the need to compare two objects of the same type (we'll use that comparison later on in specifications). TDD is supposed to help us design better and write better code. Writing 15-20 specifications to define boilerplate code that could be written automatically by IDE (as is the case with the equals method) does not help us meet these objectives. Mastering TDD means not only learning how to write specifications, but also knowing when it's not worth it.

That being said, consult the source code to see the bean specification and implementation in it's entirety.

The source code can be found in the 01-bean branch of the tdd-java-ch06-tic-tac-toe-mongo Git repository (https://bitbucket.org/vfarcic/tdd-java-ch06-tic-tac-toe-mongo/branch/01-bean). The particular classes are TicTacToeBeanSpec and TicTacToeBean.

Now, let's go to a more interesting part (but still without mocks, spies, and validations). Let's write specifications related to saving data to MongoDB.

For this requirement, we'll create two new classes inside the com.packtpublishing.tddjava.ch03tictactoe.mongo package:

  • TicTacToeCollectionSpec (inside src/test/java)
  • TicTacToeCollection (inside src/main/java)
..................Content has been hidden....................

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