我正在尝试单元测试我的主持人,并使用以下代码查看Android应用程序:
@RunWith(MockitoJUnitRunner.class)
public class MovieDetailsPresenterUnitTest {
@Mock
NetworkAPI mNetworkAPI;
@Mock
NetworkService mNetworkService;
@Mock
MovieDetailsContract.View mView;
@Mock
MovieDetailsActivity mMovieDetailsActivity;
private MovieDetailsPresenter mMovieDetailsPresenter;
@Before
public void setUp() throws Exception {
this.mMovieDetailsPresenter = new MovieDetailsPresenter(mNetworkService, mMovieDetailsActivity);
}
@Rule
public final RxSchedulersOverrideRule mOverrideSchedulersRule = new RxSchedulersOverrideRule();
@Test
public void testMovieDetailsEmptyResponse() throws Exception {
MovieDetails movieDetails = new MovieDetails();
when(mNetworkAPI.getMovieDetails("abcd")).thenReturn(Observable.<MovieDetails>just(movieDetails));
mMovieDetailsPresenter.loadMovieDetails("abcd");
verify(mMovieDetailsActivity).onSupportNavigateUp();
}
@After
public void tearDown() throws Exception {
RxAndroidPlugins.getInstance().reset();
}
}
但是我得到了这个错误:
Wanted but not invoked:
mMovieDetailsActivity.onSupportNavigateUp();
-> at com.android.sushil.omdbclient.MovieDetailsPresenterUnitTest.testMovieDetailsEmptyResponse(MovieDetailsPresenterUnitTest.java:65)
Actually, there were zero interactions with this mock.
Wanted but not invoked:
mMovieDetailsActivity.onSupportNavigateUp();
-> at com.android.sushil.omdbclient.MovieDetailsPresenterUnitTest.testMovieDetailsEmptyResponse(MovieDetailsPresenterUnitTest.java:65)
Actually, there were zero interactions with this mock.
at com.android.sushil.omdbclient.MovieDetailsPresenterUnitTest.testMovieDetailsEmptyResponse(MovieDetailsPresenterUnitTest.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at com.android.sushil.omdbclient.util.RxSchedulersOverrideRule$3.evaluate(RxSchedulersOverrideRule.java:45)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
您的模拟Mnetworkapi和mview从未在任何地方注入,因此固执
when(mNetworkAPI.getMovieDetails("abcd")).thenReturn(Observable.<MovieDetails>just(movieDetails));
永远不会发生。由于我看不到Moviedetailspresenter的源代码,所以我只是猜想这种不足的源代码会导致onSupportNavigateUp()方法从未被调用的逻辑。您需要通过用@InjectMocks标记Mmoviedetailspresenter中确保所有模型都包含
@InjectMocks
private MovieDetailsPresenter mMovieDetailsPresenter;
虽然我不能100%确定这一点,直到我们看到您的更多课程的代码。