我认为我没有正确使用 verify
。以下是测试:
@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
}
这里是它调用的代码:
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;
}
失败跟踪:
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)
我想要验证的调用在多层嵌套中,有没有其他方法来检查它?
foo()
调用odp()
,然后再调用bar()
,verify(gm).bar()
仍然能够检测到它吗? - Nick Heinerfoo()
正在做的吗?我不明白有什么区别。 - Pascal Thivent