java - NullPointerException when calling mocked method -


i try mock 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 mocked method in testmymethod() in mymethod() nullpointerexception raises, why?

edit :

the complete failure trace :

java.lang.nullpointerexception     @ java.io.datainputstream.readchar(unknown source)     @ test.test.myclass.mymethod(myclass.java:8)     @ test.test.myclasstest.testmymethod(myclasstest.java:23)     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(unknown source)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source)     @ java.lang.reflect.method.invoke(unknown source)     @ org.junit.internal.runners.testmethod.invoke(testmethod.java:59)     @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl$powermockjunit44methodrunner.runtestmethod(powermockjunit44runnerdelegateimpl.java:310)     @ org.junit.internal.runners.methodroadie$2.run(methodroadie.java:79)     @ org.junit.internal.runners.methodroadie.runbeforesthentestthenafters(methodroadie.java:87)     @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl$powermockjunit44methodrunner.executetest(powermockjunit44runnerdelegateimpl.java:294)     @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl$powermockjunit44methodrunner.runbeforesthentestthenafters(powermockjunit44runnerdelegateimpl.java:282)     @ org.junit.internal.runners.methodroadie.runtest(methodroadie.java:77)     @ org.junit.internal.runners.methodroadie.run(methodroadie.java:42)     @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl.invoketestmethod(powermockjunit44runnerdelegateimpl.java:207)     @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl.runmethods(powermockjunit44runnerdelegateimpl.java:146)     @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl$1.run(powermockjunit44runnerdelegateimpl.java:120)     @ org.junit.internal.runners.classroadie.rununprotected(classroadie.java:27)     @ org.junit.internal.runners.classroadie.runprotected(classroadie.java:37)     @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl.run(powermockjunit44runnerdelegateimpl.java:122)     @ org.powermock.modules.junit4.common.internal.impl.junit4testsuitechunkerimpl.run(junit4testsuitechunkerimpl.java:104)     @ org.powermock.modules.junit4.common.internal.impl.abstractcommonpowermockrunner.run(abstractcommonpowermockrunner.java:53)     @ org.powermock.modules.junit4.powermockrunner.run(powermockrunner.java:53)     @ org.eclipse.jdt.internal.junit4.runner.junit4testreference.run(junit4testreference.java:50)     @ org.eclipse.jdt.internal.junit.runner.testexecution.run(testexecution.java:38)     @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:459)     @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:675)     @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.run(remotetestrunner.java:382)     @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.main(remotetestrunner.java:192) 

datainputstream 'system' class jvm loaded jvm. @preparefortest have remove final modifier methods (to able mock), can't already-loaded classes (hotspot jvm doesn't support class signature changes already-loaded classes), , why exception.

luckily there's datainput interface implemented datainputstream - maybe can try mocking not datainputstream datainput, don't need powermock, mockito.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -