java - NullPointerException when stubbing a test method call with Mockito -


i have these 2 lines in unit test:

defaultmessagelistenercontainer defaultmessagelistenercontainer = mockito.mock(defaultmessagelistenercontainer.class); when(defaultmessagelistenercontainer.isrunning()).thenreturn(true); 

stack trace:

java.lang.nullpointerexception     @ org.springframework.jms.listener.abstractjmslisteningcontainer.isrunning(abstractjmslisteningcontainer.java:347)     @ xxxxxxxxxxxxxx.messageprocessorcontrollertest.testping(messageprocessorcontrollertest.java:109)     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43)     @ java.lang.reflect.method.invoke(method.java:497)     @ org.testng.internal.methodinvocationhelper.invokemethod(methodinvocationhelper.java:85)     @ org.testng.internal.invoker.invokemethod(invoker.java:639)     @ org.testng.internal.invoker.invoketestmethod(invoker.java:816)     @ org.testng.internal.invoker.invoketestmethods(invoker.java:1124)     @ org.testng.internal.testmethodworker.invoketestmethods(testmethodworker.java:125)     @ org.testng.internal.testmethodworker.run(testmethodworker.java:108)     @ org.testng.testrunner.privaterun(testrunner.java:774)     @ org.testng.testrunner.run(testrunner.java:624)     @ org.testng.suiterunner.runtest(suiterunner.java:359)     @ org.testng.suiterunner.runsequentially(suiterunner.java:354)     @ org.testng.suiterunner.privaterun(suiterunner.java:312)     @ org.testng.suiterunner.run(suiterunner.java:261)     @ org.testng.suiterunnerworker.runsuite(suiterunnerworker.java:52)     @ org.testng.suiterunnerworker.run(suiterunnerworker.java:86)     @ org.testng.testng.runsuitessequentially(testng.java:1191)     @ org.testng.testng.runsuiteslocally(testng.java:1116)     @ org.testng.testng.run(testng.java:1024)     @ org.testng.idearemotetestng.run(idearemotetestng.java:72)     @ org.testng.remotetestngstarter.main(remotetestngstarter.java:124) 

no matter do, npe thrown on when line if real method getting called when setting stub. using mockito v1.10.19. doing wrong?

update: here entire standalone test class:

import org.mockito.mockito; import org.springframework.jms.listener.defaultmessagelistenercontainer; import org.testng.annotations.test;  import static org.mockito.mockito.when;     public class jmstest {     @test     public void test() throws exception {         defaultmessagelistenercontainer defaultmessagelistenercontainer = mockito.mock(                 defaultmessagelistenercontainer.class);         when(defaultmessagelistenercontainer.isrunning()).thenreturn(true);     } } 

and exception thrown:

java.lang.nullpointerexception     @ org.springframework.jms.listener.abstractjmslisteningcontainer.isrunning(abstractjmslisteningcontainer.java:347)     @ xxxxxxxxxxx.util.jmstest.test(jmstest.java:17)     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43)     @ java.lang.reflect.method.invoke(method.java:497)     @ org.testng.internal.methodinvocationhelper.invokemethod(methodinvocationhelper.java:85)     @ org.testng.internal.invoker.invokemethod(invoker.java:639)     @ org.testng.internal.invoker.invoketestmethod(invoker.java:816)     @ org.testng.internal.invoker.invoketestmethods(invoker.java:1124)     @ org.testng.internal.testmethodworker.invoketestmethods(testmethodworker.java:125)     @ org.testng.internal.testmethodworker.run(testmethodworker.java:108)     @ org.testng.testrunner.privaterun(testrunner.java:774)     @ org.testng.testrunner.run(testrunner.java:624)     @ org.testng.suiterunner.runtest(suiterunner.java:359)     @ org.testng.suiterunner.runsequentially(suiterunner.java:354)     @ org.testng.suiterunner.privaterun(suiterunner.java:312)     @ org.testng.suiterunner.run(suiterunner.java:261)     @ org.testng.suiterunnerworker.runsuite(suiterunnerworker.java:52)     @ org.testng.suiterunnerworker.run(suiterunnerworker.java:86)     @ org.testng.testng.runsuitessequentially(testng.java:1191)     @ org.testng.testng.runsuiteslocally(testng.java:1116)     @ org.testng.testng.run(testng.java:1024)     @ org.testng.idearemotetestng.run(idearemotetestng.java:72)     @ org.testng.remotetestngstarter.main(remotetestngstarter.java:124)     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43)     @ java.lang.reflect.method.invoke(method.java:497)     @ com.intellij.rt.execution.application.appmain.main(appmain.java:147) 

your defaultmessagelistenercontainer not 1 being used. failing 1 of type abstractjmslisteningcontainer.

you need make sure mocked variable correctly passed class trying test.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -