The integration test

We'll create a TicTacToeInteg class inside the com.packtpublishing.tddjava.ch03tictactoe package in the src/test/java directory. Since we know that Jongo throws an exception if it cannot connect to the database, a test class can be as simple as the following:

import org.junit.Test;
import java.net.UnknownHostException;
import static org.junit.Assert.*;

public class TicTacToeInteg {

  @Test
  public void givenMongoDbIsRunningWhenPlayThenNoException()
throws UnknownHostException { TicTacToe ticTacToe = new TicTacToe(); assertEquals(TicTacToe.NO_WINNER, ticTacToe.play(1, 1)); } }

The invocation of assertEquals is just as a precaution. The real objective of this test is to make sure that no Exception is thrown. Since we did not start MongoDB (unless you are very proactive and did it yourself, in which case you should stop it), test should fail:

Now that we know that the integration test works, or in other words, that it indeed fails when MongoDB is not up and running, let us try it again with the DB started. To bring up MongoDB, we'll use Vagrant to create a virtual machine with Ubuntu OS. MongoDB will be run as a Docker.

Make sure that the 04-integration branch is checked out:

From the command prompt, run the following command:

$ vagrant up
  

Be patient until VM is up and running (it might take a while when executed for the first time, especially on a slower bandwidth). Once finished, rerun integration tests:

It worked, and now we're confident that we are indeed integrated with MongoDB.

This was a very simplistic integration test, and in the real-world, we would do a bit more than this single test. We could, for example, query the DB and confirm that data was stored correctly. However, the purpose of this chapter was to learn both how to mock and that we should not depend only on unit tests. The next chapter will explore integration and functional tests in more depth.

The source code can be found in the 04-integration branch of the tdd-java-ch06-tic-tac-toe-mongo Git repository (https://bitbucket.org/vfarcic/tdd-java-ch06-tic-tac-toe-mongo/branch/04-integration).

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

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