Gualtiero Testa (17) [Avatar] Offline

in the listing 3.10, the value (the entity id) returned by
is not checked.

This is somehow understandable because the entity manager is mocked but I don’t find very clean, at least for a first Mockito example.

In general I think the return values should be the first think to check and then we can also check the side-effects (the call to the merge method by Mockito.verify).

May be it is worth to mention this to the reader.

lucacin (5) [Avatar] Offline
One possible solution to be aligned with the paragraph:

"And finally apart from asserting the expected value, the test verifies that the mocked method is called too. This is important to be sure that class under test calls the expected dependency methods and not another ones not related with the test."

	public void shouldCreateAGame() {
		final long gameId = 123L;
		final Game game = new Game();

		final Games games = new Games();

		when(entityManager.merge(game)).thenReturn(game); // <3>
		games.em = entityManager; // <4>

		final Long obtainedGameId = games.create(game); // <5>

		verify(entityManager).merge(game); // <6>