在尝试模拟InetAddress中的静态方法时,我遇到了奇怪的问题。我成功地能够为许多其他类模拟静态方法,并且一切正常,但InetAddress显示不同的行为。我使用的是JUnit 4. x、Mockito 1.9.5
下面给出了使用Mockito和PowerMock以及InetAddress模拟的测试-
@RunWith(PowerMockRunner.class)
@PrepareForTest({InetAddress.class})
public class UtilityTest {
@Mock
InetAddress inetAddress;
@Test
public void testGetCurrentHost() throws UnknownHostException {
mockStatic(InetAddress.class);
when(InetAddress.getLocalHost()).thenReturn(inetAddress);
when(inetAddress.getHostAddress()).thenReturn("1.1.1.1");
assertEquals("1.1.1.1", getCurrentHost());
}
private static String getCurrentHost() throws UnknownHostException {
return InetAddress.getLocalHost().getHostAddress();
}
}
当我将下面给出的方法放入某个实用程序InetAddress中时。getLocalHost()不再模拟,测试失败。
将其移动到实用程序:
private static String getCurrentHost() throws UnknownHostException {
return InetAddress.getLocalHost().getHostAddress();
}
现在,我的测试看起来(失败):
@RunWith(PowerMockRunner.class)
@PrepareForTest({InetAddress.class})
public class UtilityTest {
@Mock
InetAddress inetAddress;
@Test
public void testGetCurrentHost() throws UnknownHostException {
mockStatic(InetAddress.class);
when(InetAddress.getLocalHost()).thenReturn(inetAddress);
when(inetAddress.getHostAddress()).thenReturn("1.1.1.1");
assertEquals("1.1.1.1", Utility.getCurrentHost());
}
}
下面给出的是我的堆栈跟踪-
org.junit.ComparisonFailure:
Expected :1.1.1.1
Actual :192.111.111.121
<Click to see difference>
at org.junit.Assert.assertEquals(Assert.java:115)
at org.junit.Assert.assertEquals(Assert.java:144)
at com.somecompany.helper.INetTest.testGetCurrentHost(INetTest.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:88)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:96)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:104)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Process finished with exit code -1
我找到了解决办法。将PrepareForTest({InetAddress.class})更改为PrepareForTest({Utility.class,InetAddress.class})
我知道这个帖子有点旧,但我找到了一个解决方案。
您必须执行@PrepareForTest({InetAddress.class})
,而必须执行@PrepareForTest({YouClassThatWrap.class})
。
在这里(https://code.google.com/p/powermock/wiki/MockSystem)它展示了如何模拟SystemClass。
代码@准备测试({InetAddress.class})
将不起作用,因为Powermock无法更改系统类代码,因此您需要模拟“包装器”类。
问题内容: 我正在尝试模拟私有静态方法。见下面的代码 这是我的测试代码 但是我运行的每个瓦片都会出现此异常 我想我在嘲弄东西时做错了什么。有什么想法我该如何解决? 问题答案: 为此,您可以使用和。 此外,您必须在测试类中指定PowerMock运行器,并准备要进行测试的类,如下所示: 希望对您有帮助。
当所有参数都在使用匹配器时,为什么我会得到这个异常?如何解决?我调试了它,发现返回null。 当我将注释添加到测试类并运行测试时,junit不会做出响应。为什么? 编辑 我试着不使用论据匹配器,结果
PowerMockito.when(ConnectionFactory.getConn(“ABC”).getCurrentStatus()).thenReturn(ConnectionStatus.Connected); 对于上面的陈述,我得到了一个NPE。 我在junit测试类的开头已经有@PrepareForTest({fxallConnectionFactory.class,Connecti
我使用web上的各种解决方案尝试了这个解决方案,但无法找到代码中的错误。我需要为静态方法调用存根,因为我需要它用于遗留代码。如何使用PowerMockito模拟静态方法?
编辑: 感谢所有的回复。我找到了解决办法。我试图模拟一个方法findById,它不是直接在User.class中,而是在用户扩展的genericModel.class中。现在一切都很完美。