Java Mockito的。当通话似乎增加通话次数时。
使用以下代码:Java Mockito的。当通话似乎增加通话次数时。,java,unit-testing,junit,mockito,Java,Unit Testing,Junit,Mockito,使用以下代码: @Test public void testSetGameType_deathmatch() { CommandSender sender = Mockito.mock(CommandSender.class); String[] args = {"deathmatch"}; Pvpgames plugin = PowerMockito.mock(Pvpgames.class, Mockito.withSettings().verboseLogging()
@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方法中的调用时。我是否做错了什么,或者这是mockito中的一个bug?这两行:PowerMockito.whenplugin.getServer.getPluginManager.thenReturnpluginManager;PowerMockito.whenplugin.getServer.broadcastMessageorg.mockito.Matchers.anyString.thenReturn1;应该是这样的:PowerMockito.whenserver.getPluginManager.thenReturnpluginManager;PowerMockito.whenserver.broadcastMessageorg.mockito.Matchers.anyString.thenReturn1@Mindaugas谢谢,但这并不能解决问题。
############ 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)