I have 2 completables in a method, like:
completable1.doSomething()
.andThen(completable2.doSomethingElse())
And I want to test it, by using Mockito and returning error in the first completable. Here is my original method:
public Completable updateQuestion(Question question){
return gameRepositoryType.updateQuestion(question)
.andThen(gameRepositoryType.getGameById(question.getqId())
.map(GAME_MAPPER)
.flatMapCompletable(gameRepositoryType::updateGame))
.observeOn(AndroidSchedulers.mainThread());
}
And here is my test:
@Test
public void updateQuestionError(){
when(gameRepositoryType.updateQuestion(question)).thenReturn(Completable.error(error));
when(question.getqId()).thenReturn(FAKE_QID);
when(gameRepositoryType.getGameById(FAKE_QID)).thenReturn(Single.just(game));
when(gameRepositoryType.updateGame(game)).thenReturn(Completable.complete());
interactor.updateQuestion(question)
.test()
.assertNotComplete()
.assertError(error);
verify(gameRepositoryType).updateQuestion(question); //this is called in test
verify(question,never()).getqId(); //this is called in test
verify(gameRepositoryType,never()).getGameById(FAKE_QID); //this is called in test
verify(gameRepositoryType,never()).updateGame(game); //this is NEVER called in test
}
In my verifications at the end of the test method, I am expecting after the first completable emitts an error, the 3 last verifications should run with success, i.e the 3 last verify() should never be called.
However I can see that all methods are called even though gameRepositoryType.updateQuestion(question) emitts error. Why is that?
Shouldn't andThen() never be called if the first completable emitts error?