使用以下代码:
@Test
public void testSetGameType_deathmatch() {
CommandSender sender = Mockito.mock(CommandSender.class);
String[] args = {"deathmatch"};
Pvpgames plugin = PowerMockito.mock(Pvpgames.class, Mockito.withSettings().verboseLogging());
plugin.settings = new HashMap<>();
Server server = PowerMockito.mock(Server.class);
PluginManager pluginManager = PowerMockito.mock(PluginManager.class);
PowerMockito.when(plugin.getServer()).thenReturn(server);
PowerMockito.when(plugin.getServer().getPluginManager()).thenReturn(pluginManager);
PowerMockito.when(plugin.getServer().broadcastMessage(org.mockito.Matchers.anyString())).thenReturn(1);
PowerMockito.when(plugin.getGameType()).thenCallRealMethod();
Mockito.when(plugin.setActiveListener(Mockito.any())).thenCallRealMethod();
boolean result = GametypeHandler.setGameType(sender, args, plugin);
Assert.assertTrue(result);
Mockito.verify(plugin).setActiveListener(Mockito.any(DeathMatchListener.class));
Mockito.verify(plugin.getServer().getPluginManager()).registerEvents(Mockito.any(DeathMatchListener.class), Mockito.same(plugin));
Mockito.verify(sender).sendMessage(Mockito.anyString());
Mockito.verify(plugin.getServer()).broadcastMessage("Gametype has been changed to: Deathmatch");
}
当我验证Mockito.verify(plugin).setActiveListener(Mockito.any(DeathMatchListener.class));
时,.setActiveListener显然被调用了3次,但是使用我的IDE跟踪它会告诉我它只被调用过一次。
所以我在mockito中启用了详细日志记录,并将其作为输出:
############ Logging method invocation #1 on mock/spy ########
pvpgames.getGameType();
invoked: -> at dk.zlepper.pvpgames.commands.GametypeHandlerTest.testSetGameType_deathmatch(GametypeHandlerTest.java:57)
has returned: "null"
############ Logging method invocation #2 on mock/spy ########
pvpgames.setActiveListener(null);
invoked: -> at dk.zlepper.pvpgames.commands.GametypeHandlerTest.testSetGameType_deathmatch(GametypeHandlerTest.java:58)
has returned: "null"
############ Logging method invocation #3 on mock/spy ########
stubbed: -> at dk.zlepper.pvpgames.commands.GametypeHandlerTest.testSetGameType_deathmatch(GametypeHandlerTest.java:58)
pvpgames.setActiveListener(
dk.zlepper.pvpgames.listeners.DeathMatchListener@3ddc6915
);
invoked: -> at dk.zlepper.pvpgames.commands.GametypeHandler.setGameType(GametypeHandler.java:22)
has returned: "dk.zlepper.pvpgames.listeners.DeathMatchListener@3ddc6915" (dk.zlepper.pvpgames.listeners.DeathMatchListener)
############ Logging method invocation #4 on mock/spy ########
stubbed: -> at dk.zlepper.pvpgames.commands.GametypeHandlerTest.testSetGameType_deathmatch(GametypeHandlerTest.java:58)
pvpgames.setActiveListener(
dk.zlepper.pvpgames.listeners.DeathMatchListener@3ddc6915
);
invoked: -> at dk.zlepper.pvpgames.commands.GametypeHandler.setGameType(GametypeHandler.java:22)
has returned: "dk.zlepper.pvpgames.listeners.DeathMatchListener@3ddc6915" (dk.zlepper.pvpgames.listeners.DeathMatchListener)
############ Logging method invocation #5 on mock/spy ########
stubbed: -> at dk.zlepper.pvpgames.commands.GametypeHandlerTest.testSetGameType_deathmatch(GametypeHandlerTest.java:57)
pvpgames.getGameType();
invoked: -> at dk.zlepper.pvpgames.commands.GametypeHandler.setGameType(GametypeHandler.java:37)
has returned: "Deathmatch" (dk.zlepper.pvpgames.commands.GameType)
############ Logging method invocation #6 on mock/spy ########
stubbed: -> at dk.zlepper.pvpgames.commands.GametypeHandlerTest.testSetGameType_deathmatch(GametypeHandlerTest.java:57)
pvpgames.getGameType();
invoked: -> at dk.zlepper.pvpgames.commands.GametypeHandler.setGameType(GametypeHandler.java:37)
has returned: "Deathmatch" (dk.zlepper.pvpgames.commands.GameType)
############ Logging method invocation #7 on mock/spy ########
pvpgames.setActiveListener(null);
invoked: -> at dk.zlepper.pvpgames.commands.GametypeHandlerTest.testSetGameType_deathmatch(GametypeHandlerTest.java:63)
has thrown: class org.mockito.exceptions.verification.TooManyActualInvocations with message
pvpgames.setActiveListener(null);
Wanted 1 time:
-> at dk.zlepper.pvpgames.commands.GametypeHandlerTest.testSetGameType_deathmatch(GametypeHandlerTest.java:63)
But was 3 times. Undesired invocation:
-> at dk.zlepper.pvpgames.commands.GametypeHandler.setGameType(GametypeHandler.java:22)
所以看起来mockito算是自己的。当在调用方法中作为调用调用时。我做错了什么,或者这是mockito中的错误?