调用mocked方法时出现NullPointerException

时间:2015-03-28 14:42:40

标签: java mockito powermock

我尝试模拟最终方法(类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()中调用mocked方法时有效,但在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$2.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$1.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)

2 个答案:

答案 0 :(得分:2)

首先这段代码是一个模拟反模式:不要模仿你不拥有的模型!(见answer on StackOverflow

其次,DataInputStream是JDK的一个类PowerMock不能使用修改字节代码的 hacky 类加载器。

为此,有一个解决方案和两个可能的技巧:

  • 使用界面
  • 将调用封装在您拥有的类中,然后准备此类,在google code处记录,或者甚至更好地创建自己的可模拟类(不需要powermock)
  • 使用google code
  • 中记录的代理

第一个选项显然是最好的,前两个选项也允许避免这种模拟反模式。

答案 1 :(得分:1)

DataInputStream是来自JVM的“系统”类,可能已由JVM加载。 @PrepareForTest必须从方法中移除final修饰符(以便能够模拟),但是对于已经加载的类,它不能这样做(HotSpot JVM不支持类签名更改已经这可能是你得到这个例外的原因。

幸运的是,DataInput也实现了DataInputStream接口 - 也许你可以尝试嘲笑DataInputStream而不是DataInput,为此你甚至不需要PowerMock,只需要Mockito