java - Mock a protected method in an abstract class -



java - Mock a protected method in an abstract class -

i have abstract class protected method trying mock. please note abstract class defined in company's library api have use.

public abstract class supportobjectbase { protected nrobject getnrobjects(order order) throws applicationexception { homecoming ... ; } }

the issue nullpointerexception when seek unit test code uses protected method within abstract class. exception stack-trace (n.b. minimal due intellectual property).

also, line 153 in supportobjectbase declaration of protected method getnrobjects(order).

java.lang.nullpointerexception @ xx.xxxx.xxxxxx.xxxxxx.supportobjectbase.getnrobjects(supportobjectbase.java:153) @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl$powermockjunit44methodrunner.runtestmethod(powermockjunit44runnerdelegateimpl.java:310) @ org.junit.internal.runners.methodroadie$2.run(methodroadie.java:88) @ org.junit.internal.runners.methodroadie.runbeforesthentestthenafters(methodroadie.java:96) @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl$powermockjunit44methodrunner.executetest(powermockjunit44runnerdelegateimpl.java:294) @ org.powermock.modules.junit4.internal.impl.powermockjunit47runnerdelegateimpl$powermockjunit47methodrunner.executetestinsuper(powermockjunit47runnerdelegateimpl.java:127) @ org.powermock.modules.junit4.internal.impl.powermockjunit47runnerdelegateimpl$powermockjunit47methodrunner.access$100(powermockjunit47runnerdelegateimpl.java:59) @ org.powermock.modules.junit4.internal.impl.powermockjunit47runnerdelegateimpl$powermockjunit47methodrunner$lastruletestexecutorstatement.evaluate(powermockjunit47runnerdelegateimpl.java:148) @ org.junit.rules.expectedexception$expectedexceptionstatement.evaluate(expectedexception.java:168) @ org.powermock.modules.junit4.internal.impl.powermockjunit47runnerdelegateimpl$powermockjunit47methodrunner.executetest(powermockjunit47runnerdelegateimpl.java:91) @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl$powermockjunit44methodrunner.runbeforesthentestthenafters(powermockjunit44runnerdelegateimpl.java:282) @ org.junit.internal.runners.methodroadie.runtest(methodroadie.java:86) @ org.junit.internal.runners.methodroadie.run(methodroadie.java:49) @ 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:33) @ org.junit.internal.runners.classroadie.runprotected(classroadie.java:45) @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl.run(powermockjunit44runnerdelegateimpl.java:118) @ 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.gradle.api.internal.tasks.testing.junit.junittestclassexecuter.runtestclass(junittestclassexecuter.java:86) @ org.gradle.api.internal.tasks.testing.junit.junittestclassexecuter.execute(junittestclassexecuter.java:49) @ org.gradle.api.internal.tasks.testing.junit.junittestclassprocessor.processtestclass(junittestclassprocessor.java:69) @ org.gradle.api.internal.tasks.testing.suitetestclassprocessor.processtestclass(suitetestclassprocessor.java:48) @ 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:483) @ org.gradle.messaging.dispatch.reflectiondispatch.dispatch(reflectiondispatch.java:35) @ org.gradle.messaging.dispatch.reflectiondispatch.dispatch(reflectiondispatch.java:24) @ org.gradle.messaging.dispatch.contextclassloaderdispatch.dispatch(contextclassloaderdispatch.java:32) @ org.gradle.messaging.dispatch.proxydispatchadapter$dispatchinginvocationhandler.invoke(proxydispatchadapter.java:93) @ com.sun.proxy.$proxy2.processtestclass(unknown source) @ org.gradle.api.internal.tasks.testing.worker.testworker.processtestclass(testworker.java:105) @ 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:483) @ org.gradle.messaging.dispatch.reflectiondispatch.dispatch(reflectiondispatch.java:35) @ org.gradle.messaging.dispatch.reflectiondispatch.dispatch(reflectiondispatch.java:24) @ org.gradle.messaging.remote.internal.hub.messagehub$handler.run(messagehub.java:355) @ org.gradle.internal.concurrent.defaultexecutorfactory$stoppableexecutorimpl$1.run(defaultexecutorfactory.java:64) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617) @ java.lang.thread.run(thread.java:745)

yes, unit testing showing code has been poorly designed - but, there way mock protected method defined in abstract class? (i have no thought why set concrete code in protected method within abstract class).

you can below

public class supportobjectbasetest { @test public void getnrobjects_shoulddosomething() { supportbaseobject object = mockito.mock(supportobjectbase.class, mockito.calls_real_methods); //assert below //assert.assertreturn(object.getnrobject(new order())); } }

note give idea. have modify based on method implementation

java unit-testing mockito

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -