Using the following code:
@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");
}
When I verify Mockito.verify(plugin).setActiveListener(Mockito.any(DeathMatchListener.class));
.setActiveListener has apparently been called 3 times, however tracing it with my IDE tells me it has only been called once. So I enabled verbose logging in mockito and I get this as output:
############ 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)
So it seems like mockito counts it's own .when call as a call in the verify method. Have I done something wrong, or is this a bug in mockito?
Aucun commentaire:
Enregistrer un commentaire