NullPointerException when calling mocked method

csa

I try to mock a final method (readChar of class DataInputStream):

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
    }
}

It works when calling the mocked method in testMyMethod() but in myMethod() NullPointerException raises, why?

EDIT :

The complete failure trace :

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)
iirekm

DataInputStream is a 'system' class from JVM which is probably already loaded by JVM. @PrepareForTest would have to remove final modifier from the methods (to be able to mock), but it can't do so for already-loaded classes (HotSpot JVM doesn't support class signature changes for already-loaded classes), and this is probably why you get this exception.

Luckily there's also DataInput interface implemented by DataInputStream - maybe you can try mocking not DataInputStream but DataInput, for this you don't even need PowerMock, just Mockito.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

NullPointerException when calling a method reference to an arbitrary object with null argument

From Java

Mocked method returning NullPointerException

From Java

Getting java.lang.NullPointerException when calling Method.invoke

From Java

NullPointerException when calling method from another class

From Java

Spock - mocked repository method save() giving NullPointerException

From Dev

PowerMockito is calling real method instead of mocked private one

From Dev

NullPointerException when trying to call method on mocked Eclipse Paho MqttClient

From Dev

When Calling Other Activity Method Showing NullPointerException

From Dev

Getting NullPointerException on calling getter method

From Dev

NRE in a unit test when await a mocked method

From Dev

Mockito : java.lang.NullPointerException when calling method from mocked Class

From Dev

Static method mocked via EasyMock and Powermock getting invoked when calling EasyMock.expect()

From Dev

NullPointerException when calling a method from a different class

From Dev

NullPointerException when calling a method from a dynamic object array

From Dev

NullPointerException when calling GetText()

From Dev

Android ViewPager NullPointerException by calling a method

From Dev

why is mockito not called when executing mocked method?

From Dev

NullPointerException when calling a Fragment method from Activity?

From Dev

Calling method of object property in mocked object results in NullPointerException

From Dev

Spying a method is calling the actual method instead of the mocked one

From Dev

NullPointerException when calling onClickListener

From Dev

NullPointerException when calling EJB method

From Dev

NullPointerException when calling Redis method

From Dev

Minitest - calling a mocked instance method from within another instance method

From Dev

hibernate throws NullPointerException when calling query.list method

From Dev

Groovy Spock mock calling real method of mocked class

From Dev

SpringBoot @Autowired NullPointerException when calling method from service class

From Dev

hibernate - why mocked method throws NullPointerException

From Dev

NullPointerException when calling a mocked method

Related Related

  1. 1

    NullPointerException when calling a method reference to an arbitrary object with null argument

  2. 2

    Mocked method returning NullPointerException

  3. 3

    Getting java.lang.NullPointerException when calling Method.invoke

  4. 4

    NullPointerException when calling method from another class

  5. 5

    Spock - mocked repository method save() giving NullPointerException

  6. 6

    PowerMockito is calling real method instead of mocked private one

  7. 7

    NullPointerException when trying to call method on mocked Eclipse Paho MqttClient

  8. 8

    When Calling Other Activity Method Showing NullPointerException

  9. 9

    Getting NullPointerException on calling getter method

  10. 10

    NRE in a unit test when await a mocked method

  11. 11

    Mockito : java.lang.NullPointerException when calling method from mocked Class

  12. 12

    Static method mocked via EasyMock and Powermock getting invoked when calling EasyMock.expect()

  13. 13

    NullPointerException when calling a method from a different class

  14. 14

    NullPointerException when calling a method from a dynamic object array

  15. 15

    NullPointerException when calling GetText()

  16. 16

    Android ViewPager NullPointerException by calling a method

  17. 17

    why is mockito not called when executing mocked method?

  18. 18

    NullPointerException when calling a Fragment method from Activity?

  19. 19

    Calling method of object property in mocked object results in NullPointerException

  20. 20

    Spying a method is calling the actual method instead of the mocked one

  21. 21

    NullPointerException when calling onClickListener

  22. 22

    NullPointerException when calling EJB method

  23. 23

    NullPointerException when calling Redis method

  24. 24

    Minitest - calling a mocked instance method from within another instance method

  25. 25

    hibernate throws NullPointerException when calling query.list method

  26. 26

    Groovy Spock mock calling real method of mocked class

  27. 27

    SpringBoot @Autowired NullPointerException when calling method from service class

  28. 28

    hibernate - why mocked method throws NullPointerException

  29. 29

    NullPointerException when calling a mocked method

HotTag

Archive