Gualtiero Testa (17) [Avatar] Offline
#1
Hi,

in the listing 3.10, the value (the entity id) returned by
games.create(game)
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.

Gualtiero
lucacin (5) [Avatar] Offline
#2
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."

@Test
	public void shouldCreateAGame() {
		final long gameId = 123L;
		final Game game = new Game();
		game.setId(gameId);
		game.setTitle("Zelda");

		final Games games = new Games();

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

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

		Assertions.assertThat(obtainedGameId).isEqualTo(gameId);
		verify(entityManager).merge(game); // <6>

	}