0

I am writing an unit test for methodOne and am trying to simulate an exception scenario.

 Single<String> methodOne() {
     methodTwo();
     return Single.just("Hello");
}

Single<String> methodTwo() {
//some network calls
return Single.just("World");
}

I see that there are two ways to do this
when(test.methodTwo()).thenReturn(Single.error(new BadRequestException("bad request"));

when(test.methodTwo()).thenThrow(new BadRequestException("bad request"));

What is the difference between the two?

Trying to understand the difference between the above

eglease
  • 2,445
  • 11
  • 18
  • 28
  • Does this answer your question? [RxJava - Using Single.Error / Observable.error vs throwing exception](https://stackoverflow.com/questions/61659591/rxjava-using-single-error-observable-error-vs-throwing-exception) – Gustavo Pagani Aug 04 '23 at 18:46
  • If you use Single.error(new Exception) the exception will never be raised, because you never subscribe ot the result of methodTwo. In the second case the test with fail with the thrown exception, because it's not wrapped into a Single – Daniil Aug 30 '23 at 16:10

0 Answers0