2017-05-24 90 views
0

請幫我這個當我運行所有的類測試方法一起測試,儘管這種方法的方法失敗時,我獨自運行它,它成功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()); 
    })); 
} 

}

+0

看來你忘了展示測試 –

+0

我編輯的問題抱歉.. –

回答

3

我發現會出現此錯誤,因爲它是在RX Scheulers 問題之前,你應該重新設置AndroidSchedulers.mainThread()返回的默認調度程序是LooperScheduler的一個實例,它依賴於JUnit測試中不可用的Android依賴項。

我們可以通過在測試運行之前使用不同的Scheduler初始化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(); }} 
+0

感謝。我有同樣的問題,這已經解決了。 – DanXPrado

1

我不知道代碼本身 但如果每個測試運行成功,如果你運行它只是 和失敗,如果它與運行其他測試

然後,如果測試用例共享某些數據,運行在相同的上下文中或訪問相同的數據庫,緩存,文件或任何存儲庫,則可能會對其他測試用例造成損壞 每個測試用例當你正在使用應的Mockito與其他測試用例

無依賴性成功運行,現在看來,這是 則應重嘲笑每個測試用例對象(即@Before) 它的上下文的問題Mockito提供了一個重置​​方法 Mockito.reset(mRealEstatesView);

問題是的Mockito計算在所有測試用例對象全部命中,所以每個測試用例

相關問題