Android单元测试运行所有类测试失败,但每个方法都成功运行

时间:2017-05-24 23:21:19

标签: android unit-testing mockito android-testing rx-android

当我一起运行所有类测试方法时,请帮助我这个测试在一个方法中失败,虽然这个方法当我单独运行它成功时

public class RealEstatesListPresenterTest {
RealEstatesListPresenter mRealEstatesListPresenter;
@Mock
private RealEstateListBusiness mRealEstateListBusiness;
@Mock
private RealEstatesListContract.View mRealEstatesView;

@BeforeClass
public static void setUpClass() {
    RxAndroidPlugins.setInitMainThreadSchedulerHandler(__ -> Schedulers.trampoline());
}

@Before
public void setupTasksPresenter() {
    MockitoAnnotations.initMocks(this);
    mRealEstatesListPresenter = new RealEstatesListPresenter(mRealEstatesView);
    mRealEstatesListPresenter.setmRealEstateListBusiness(mRealEstateListBusiness);
}

@Test
public void testWhenGetAllRealEstates_ProgressISDisplayed() {
    when(mRealEstateListBusiness.getAllRealEstates()).thenReturn(Observable.create(sub -> {
        sub.onNext(new ArrayList<>());
        sub.onComplete();
    }));
    mRealEstatesListPresenter.getAllRealEstates();
    verify(mRealEstatesView, times(1)).showLoading();
}

@Test
public void testWhenGetAllRealEstatesSuccess_ProgressISHidden() {
    when(mRealEstateListBusiness.getAllRealEstates()).thenReturn(Observable.create(sub -> {
        sub.onNext(new ArrayList<>());
        sub.onComplete();
    }));
    mRealEstatesListPresenter.getAllRealEstates();
    verify(mRealEstatesView, times(1)).hideLoading();
}

@Test
public void testWhenGetAllRealEstatesError_ProgressISHidden() {
    when(mRealEstateListBusiness.getAllRealEstates()).thenReturn(Observable.create(sub -> {
        sub.onError(new Throwable());
    }));
    mRealEstatesListPresenter.getAllRealEstates();
    verify(mRealEstatesView, times(1)).hideLoading();
}

@AfterClass
public static void tearDownClass() {
    RxAndroidPlugins.reset();
}}

当我一起运行所有测试时,前两个方法通过,但最后一个方法失败(testWhenGetAllRealEstatesError_ProgressISHidden),但是当我单独运行它时,它会通过。

这是演示者代码

public class RealEstatesListPresenter implements RealEstatesListContract.Presenter {

private RealEstatesListContract.View mView;
private RealEstateListBusiness mRealEstateListBusiness;
private CompositeDisposable mSubscriptions;

@Inject
public RealEstatesListPresenter(RealEstatesListContract.View view) {
    this.mView = view;
    mSubscriptions = new CompositeDisposable();
}

@Inject
public void setmRealEstateListBusiness(RealEstateListBusiness mRealEstateListBusiness) {
    this.mRealEstateListBusiness = mRealEstateListBusiness;
}

@Inject
public void setupListeners() {
    mView.setPresenter(this);
}

@Override
public void unSubscribe() {
    mSubscriptions.clear();
}

@Override
public void getAllRealEstates() {
    mView.showLoading();
    mSubscriptions.add(mRealEstateListBusiness.getAllRealEstates().observeOn(AndroidSchedulers.
            mainThread()).subscribeOn(Schedulers.io()).subscribe((realEstatesItems) -> {
        mView.hideLoading();
        mView.showAllRealEstates(realEstatesItems);
    }, throwable -> {
        mView.hideLoading();
        mView.showErrorMessage(throwable.getMessage());
    }));
}

}

3 个答案:

答案 0 :(得分:6)

我在RX Scheulers中发现了问题 发生此错误是因为AndroidSchedulers.mainThread()返回的默认调度程序是LooperScheduler的实例,并依赖于JUnit测试中不可用的Android依赖项。

我们可以通过在运行测试之前使用不同的调度程序初始化RxAndroidPlugins来避免此问题。你可以在@BeforeClass中做到这一点,所以最终的类就像这样

public class RealEstatesListPresenterTest {
private RealEstatesListPresenter mRealEstatesListPresenter;
@Mock
private RealEstateListBusiness mRealEstateListBusiness;
@Mock
private RealEstatesListContract.View mRealEstatesView;

@BeforeClass
public static void setUpClass() {
    Scheduler immediate = new Scheduler() {
        @Override
        public Worker createWorker() {
            return new ExecutorScheduler.ExecutorWorker(Runnable::run);
        }
    };

    RxJavaPlugins.setInitIoSchedulerHandler(scheduler -> immediate);
    RxJavaPlugins.setInitComputationSchedulerHandler(scheduler -> immediate);
    RxJavaPlugins.setInitNewThreadSchedulerHandler(scheduler -> immediate);
    RxJavaPlugins.setInitSingleSchedulerHandler(scheduler -> immediate);
    RxAndroidPlugins.setInitMainThreadSchedulerHandler(scheduler -> immediate);    }

@Before
public void setupTasksPresenter() {
    MockitoAnnotations.initMocks(this);
    mRealEstatesListPresenter = new RealEstatesListPresenter(mRealEstatesView);
    mRealEstatesListPresenter.setmRealEstateListBusiness(mRealEstateListBusiness);
}

@Test
public void testWhenGetAllRealEstates_ProgressISDisplayed() {
    when(mRealEstateListBusiness.getAllRealEstates()).thenReturn(Observable.create(sub -> {
        sub.onNext(new ArrayList<>());
        sub.onComplete();
    }));
    mRealEstatesListPresenter.getAllRealEstates();
    verify(mRealEstatesView, times(1)).showLoading();
}

@Test
public void testWhenGetAllRealEstatesSuccess_ProgressISHidden() {
    when(mRealEstateListBusiness.getAllRealEstates()).thenReturn(Observable.create(sub -> {
        sub.onNext(new ArrayList<>());
        sub.onComplete();
    }));
    mRealEstatesListPresenter.getAllRealEstates();
    verify(mRealEstatesView, times(1)).hideLoading();
}

@Test
public void testWhenGetAllRealEstatesError_ProgressISHidden() {
    when(mRealEstateListBusiness.getAllRealEstates()).thenReturn(Observable.create(sub -> {
        sub.onError(new Throwable());
    }));
    mRealEstatesListPresenter.getAllRealEstates();
    verify(mRealEstatesView, times(1)).hideLoading();
}

@Test
public void testWhenGetAllRealEstatesError_ErrorMessageDisplayed() {
    when(mRealEstateListBusiness.getAllRealEstates()).thenReturn(Observable.create(sub -> {
        sub.onError(new Throwable(""));
    }));
    mRealEstatesListPresenter.getAllRealEstates();
    verify(mRealEstatesView, times(1)).showErrorMessage("");
}

@After
public void teardown() {
    Mockito.reset(mRealEstateListBusiness);
    Mockito.reset(mRealEstatesView);
    Mockito.validateMockitoUsage();
}

@AfterClass
public static void tearDownClass() {
    RxJavaPlugins.reset();
    RxAndroidPlugins.reset();    }}

答案 1 :(得分:2)

我不确定代码本身 但如果每次测试都运行成功,那么只运行它 如果它与其他测试一起运行则失败

如果测试用例共享某些数据,在相同的上下文中运行或访问相同的db,cache,file或任何存储库,那么测试用例可能会对其他测试用例造成损坏 每个测试用例应该成功运行而不依赖于其他测试用例

当你使用Mockito时,它似乎是它的背景问题 你应该在每个测试用例上重置模拟对象(即在@Before中) Mockito提供了一种重置方法 Mockito.reset(mRealEstatesView);

问题是mockito计算了所有测试用例中该对象的所有命中,所以你应该在每个测试用例之前重置它

答案 2 :(得分:1)

您可以使用Android Test Orchestrator。如您所见here

使用AndroidJUnitRunner 1.0或更高版本时,您可以访问一个名为Android Test Orchestrator的工具,该工具可让您在自己的Instrumentation调用中运行每个应用程序的测试。

要将其添加到您的项目中:

  • 向build.gradle(:app)文件添加依赖项:

androidTestUtil "androidx.test:orchestrator:1.2.0"

  • 将以下代码添加到您的测试选项中:

testOptions { execution 'ANDROIDX_TEST_ORCHESTRATOR' }