android - Unit Test not working -
i trying write unit test test presenter function (i using mvp rx java , retrofit).
here code:
presenterunittest.java
@runwith(mockitojunitrunner.class) public class moviedetailspresenterunittest { @mock networkservice mnetworkservice; @mock moviedetailscontract.view mview; private moviedetailspresenter mmoviedetailspresenter; @before public void setup() throws exception { // override rxandroid schedulers final rxandroidplugins rxandroidplugins = rxandroidplugins.getinstance(); rxandroidplugins.registerschedulershook(new rxandroidschedulershook() { @override public scheduler getmainthreadscheduler() { return schedulers.immediate(); } }); this.mmoviedetailspresenter = new moviedetailspresenter(mnetworkservice, mview); } @test public void testmoviedetailsemptyresponse() throws exception { // given when(mnetworkservice.getmoviedetails(new networkservice.getmoviedetailscallback() { @override public void onsuccess(moviedetails moviedetailsresponse) { } @override public void onerror(throwable e) { } }, "abcd")).thenreturn(observable.just(collections.<moviedetails>emptylist())); // when mmoviedetailspresenter.loadmoviedetails("abcd"); // verify(mview).showerrorview(); } @after public void teardown() throws exception { //rxjavahooks.reset(); rxandroidplugins.getinstance().reset(); }
networkservice function:
public subscription getmoviedetails(final getmoviedetailscallback callback, string id) { return networkservice.getmoviedetails(id) .subscribeon(schedulers.io()) .observeon(androidschedulers.mainthread()) .onerrorresumenext(new func1<throwable, observable<? extends moviedetails>>() { @override public observable<? extends moviedetails> call(throwable throwable) { return observable.error(throwable); } }) .subscribe(new subscriber<moviedetails>() { @override public void oncompleted() { } @override public void onerror(throwable e) { callback.onerror(e); } @override public void onnext(moviedetails moviedetailsresponse) { callback.onsuccess(moviedetailsresponse); } }); }
moviedetailspresenter.java
public moviedetailspresenter(networkservice service, moviedetailscontract.view view) { this.mnetworkservice = service; this.mview = view; this.msubscriptions = new compositesubscription(); } @override public void loadmoviedetails(string id) { subscription subscription = mnetworkservice.getmoviedetails(new networkservice.getmoviedetailscallback() { @override public void onsuccess(moviedetails moviedetailsresponse) { log.i(tag, "loadmoviedetails : onsuccess"); mview.onmoviedetailsloadsuccess(moviedetailsresponse); } @override public void onerror(throwable e) { log.i(tag, "loadmoviedetails : onerror : " + e.getlocalizedmessage()); mview.onmoviedetailsloadfailure(e); } }, id); if(null != subscription) msubscriptions.add(subscription); }
i getting error while running test case :
java.lang.classcastexception: rx.internal.util.scalarsynchronousobservable cannot cast rx.subscription @ com.android.sushil.omdbclient.moviedetailspresenterunittest.testmoviedetailsemptyresponse(moviedetailspresenterunittest.java:104) @ 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:498) @ org.junit.runners.model.frameworkmethod$1.runreflectivecall(frameworkmethod.java:50) @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12) @ org.junit.runners.model.frameworkmethod.invokeexplosively(frameworkmethod.java:47) @ org.junit.internal.runners.statements.invokemethod.evaluate(invokemethod.java:17) @ org.junit.internal.runners.statements.runbefores.evaluate(runbefores.java:26) @ org.junit.internal.runners.statements.runafters.evaluate(runafters.java:27) @ org.junit.runners.parentrunner.runleaf(parentrunner.java:325) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:78) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:57) @ org.junit.runners.parentrunner$3.run(parentrunner.java:290) @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:71) @ org.junit.runners.parentrunner.runchildren(parentrunner.java:288) @ org.junit.runners.parentrunner.access$000(parentrunner.java:58) @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:268) @ org.junit.runners.parentrunner.run(parentrunner.java:363) @ org.mockito.internal.runners.junit45andhigherrunnerimpl.run(junit45andhigherrunnerimpl.java:37) @ org.mockito.runners.mockitojunitrunner.run(mockitojunitrunner.java:62) @ org.junit.runner.junitcore.run(junitcore.java:137) @ com.intellij.junit4.junit4ideatestrunner.startrunnerwithargs(junit4ideatestrunner.java:117) @ com.intellij.junit4.junit4ideatestrunner.startrunnerwithargs(junit4ideatestrunner.java:42) @ com.intellij.rt.execution.junit.junitstarter.preparestreamsandstart(junitstarter.java:262) @ com.intellij.rt.execution.junit.junitstarter.main(junitstarter.java:84) @ 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:498) @ com.intellij.rt.execution.application.appmain.main(appmain.java:147) org.mockito.exceptions.misusing.unfinishedstubbingexception: unfinished stubbing detected here: -> @ com.android.sushil.omdbclient.moviedetailspresenterunittest.testmoviedetailsemptyresponse(moviedetailspresenterunittest.java:94) e.g. thenreturn() may missing. examples of correct stubbing: when(mock.isok()).thenreturn(true); when(mock.isok()).thenthrow(exception); dothrow(exception).when(mock).somevoidmethod(); hints: 1. missing thenreturn() 2. trying stub final method, naughty developer! 3: stubbing behaviour of mock inside before 'thenreturn' instruction if completed @ org.mockito.internal.runners.util.frameworkusagevalidator.testfinished(frameworkusagevalidator.java:25) @ org.junit.runner.notification.synchronizedrunlistener.testfinished(synchronizedrunlistener.java:56) @ org.junit.runner.notification.runnotifier$7.notifylistener(runnotifier.java:190) @ org.junit.runner.notification.runnotifier$safenotifier.run(runnotifier.java:72) @ org.junit.runner.notification.runnotifier.firetestfinished(runnotifier.java:187) @ org.junit.internal.runners.model.eachtestnotifier.firetestfinished(eachtestnotifier.java:38) @ org.junit.runners.parentrunner.runleaf(parentrunner.java:331) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:78) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:57) @ org.junit.runners.parentrunner$3.run(parentrunner.java:290) @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:71) @ org.junit.runners.parentrunner.runchildren(parentrunner.java:288) @ org.junit.runners.parentrunner.access$000(parentrunner.java:58) @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:268) @ org.junit.runners.parentrunner.run(parentrunner.java:363) @ org.mockito.internal.runners.junit45andhigherrunnerimpl.run(junit45andhigherrunnerimpl.java:37) @ org.mockito.runners.mockitojunitrunner.run(mockitojunitrunner.java:62) @ org.junit.runner.junitcore.run(junitcore.java:137) @ com.intellij.junit4.junit4ideatestrunner.startrunnerwithargs(junit4ideatestrunner.java:117) @ com.intellij.junit4.junit4ideatestrunner.startrunnerwithargs(junit4ideatestrunner.java:42) @ com.intellij.rt.execution.junit.junitstarter.preparestreamsandstart(junitstarter.java:262) @ com.intellij.rt.execution.junit.junitstarter.main(junitstarter.java:84) @ 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:498) @ com.intellij.rt.execution.application.appmain.main(appmain.java:147)
can me resolve this. thanks
Comments
Post a Comment