调用模拟方法时出现 NullPointerException
NullPointerException when calling mocked method
我尝试模拟最终方法(class DataInputStream 的readChar):
MyClassTest
@RunWith(PowerMockRunner.class)
@PrepareForTest(DataInputStream.class)
public class MyClassTest {
@Test
public void testMyMethod() throws IOException {
DataInputStream mockStream = PowerMockito.mock(DataInputStream.class);
Mockito.when(mockStream.readChar()).thenReturn('a');
System.out.println(mockStream.readChar()); // OK (print 'a')
Assert.assertEquals('a', MyClass.myMethod(mockStream));
}
}
MyClass
public class MyClass {
public static char myMethod(DataInputStream dis) throws IOException {
return dis.readChar(); // NPE raises
}
}
在 testMyMethod() 中调用模拟方法时有效,但在 myMethod() 中引发 NullPointerException,为什么?
编辑:
完整的故障跟踪:
java.lang.NullPointerException
at java.io.DataInputStream.readChar(Unknown Source)
at test.test.MyClass.myMethod(MyClass.java:8)
at test.test.MyClassTest.testMyMethod(MyClassTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:79)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
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.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
DataInputStream
是来自 JVM 的 'system' class,它可能已经被 JVM 加载了。
@PrepareForTest
必须从方法中删除 final
修饰符(以便能够模拟),但它不能对已经加载的 classes 这样做(HotSpot JVM 不支持class 已加载的签名更改 classes),这可能就是您得到此异常的原因。
幸运的是 DataInputStream
也实现了 DataInput
接口——也许你可以尝试模拟不是 DataInputStream
而是 DataInput
,为此你甚至不需要 PowerMock,只是 Mockito。
首先,此代码是一个模拟反模式:不要模拟您不拥有的类型!(参见此 )
其次,DataInputStream
是 class 的 JDK PowerMock 不能使用相同的 hacky classloader 修改字节码。
为此,有一个解决方案和两个可能的技巧:
- 使用界面
- 将调用封装在您拥有的 class 中,然后准备这个 class,记录在此处 google code,或者更好地制作您自己的可模拟 class (不需要 powermock)
- 使用代理,记录在此处 google code
第一个选项显然是最好的,前两个选项也可以避免这种模拟反模式。
我尝试模拟最终方法(class DataInputStream 的readChar):
MyClassTest
@RunWith(PowerMockRunner.class)
@PrepareForTest(DataInputStream.class)
public class MyClassTest {
@Test
public void testMyMethod() throws IOException {
DataInputStream mockStream = PowerMockito.mock(DataInputStream.class);
Mockito.when(mockStream.readChar()).thenReturn('a');
System.out.println(mockStream.readChar()); // OK (print 'a')
Assert.assertEquals('a', MyClass.myMethod(mockStream));
}
}
MyClass
public class MyClass {
public static char myMethod(DataInputStream dis) throws IOException {
return dis.readChar(); // NPE raises
}
}
在 testMyMethod() 中调用模拟方法时有效,但在 myMethod() 中引发 NullPointerException,为什么?
编辑:
完整的故障跟踪:
java.lang.NullPointerException
at java.io.DataInputStream.readChar(Unknown Source)
at test.test.MyClass.myMethod(MyClass.java:8)
at test.test.MyClassTest.testMyMethod(MyClassTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:79)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
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.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
DataInputStream
是来自 JVM 的 'system' class,它可能已经被 JVM 加载了。
@PrepareForTest
必须从方法中删除 final
修饰符(以便能够模拟),但它不能对已经加载的 classes 这样做(HotSpot JVM 不支持class 已加载的签名更改 classes),这可能就是您得到此异常的原因。
幸运的是 DataInputStream
也实现了 DataInput
接口——也许你可以尝试模拟不是 DataInputStream
而是 DataInput
,为此你甚至不需要 PowerMock,只是 Mockito。
首先,此代码是一个模拟反模式:不要模拟您不拥有的类型!(参见此
其次,DataInputStream
是 class 的 JDK PowerMock 不能使用相同的 hacky classloader 修改字节码。
为此,有一个解决方案和两个可能的技巧:
- 使用界面
- 将调用封装在您拥有的 class 中,然后准备这个 class,记录在此处 google code,或者更好地制作您自己的可模拟 class (不需要 powermock)
- 使用代理,记录在此处 google code
第一个选项显然是最好的,前两个选项也可以避免这种模拟反模式。