进行单元测试时出错 - “实际上,与这个模拟器没有交互” - Android

时间:2017-09-12 12:09:47

标签: android mockito android-testing

我正在尝试对我的演示者进行单元测试,并使用以下代码在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)

1 个答案:

答案 0 :(得分:0)

您的模拟mNetworkAPI和mView永远不会被注入任何地方,因此存在

when(mNetworkAPI.getMovieDetails("abcd")).thenReturn(Observable.<MovieDetails>just(movieDetails));

永远不会发生。由于我无法看到MovieDetailsPresenter的源代码,我只是猜测这种缺乏存根会导致onSupportNavigateUp()方法的逻辑从未被调用。您需要通过使用@InjectMocks进行标记来确保所有模拟都包含在mMovieDetailsPresenter中。

@InjectMocks
private MovieDetailsPresenter mMovieDetailsPresenter;

虽然在看到更多课程的代码之前,我无法100%确定这一点。