I think I m not using verify
correctly. Here is the test:
@Mock GameMaster mockGM;
Player pWithMock;
@Before
public void setUpPlayer() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
pWithMock = new Player(mockGM);
}
@Test
public void mockDump() {
pWithMock.testDump();
verify(mockGM).emitRandom(); // fails
}
Here is the code it calls:
public boolean testDump() {
Letter t = tiles.getRandomTile();
return dump(t);
}
private boolean dump(Letter tile) {
if (! gm.canTakeDump() || tiles.count() == 0) {
return false;
}
tiles.remove(tile);
gm.takeTile(tile);
for (int i = 0; i < 3; i++) {
tiles.addTile(gm.emitRandom()); // this is the call I want to verify
}
return true;
}
Failure trace:
Wanted but not invoked:
gameMaster.emitRandom();
-> at nth.bananas.test.PlayerTest.mockDump(PlayerTest.java:66)
However, there were other interactions with this mock:
-> at nth.bananas.Player.dump(Player.java:45)
at nth.bananas.test.PlayerTest.mockDump(PlayerTest.java:66)
The call I want to verify is several layers down. Is there a different way to check this?