Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test RxJava Completable.error on Android

I have a function that returns a Completable which returns Completable.error(RuntimeException("message")) if another function fails or Completable.complete() if not.

I was trying to write a unit test for this and see that the flow is going correctly to the error and success code but in my test I cannot differentiate between them using

underTest.unregisterFromService().test().assertComplete().assertNoErrors()

Does anyone know how the Completable.error() value can be checked in unit test?

like image 541
Bootstrapper Avatar asked Oct 16 '25 18:10

Bootstrapper


2 Answers

I believe what you're looking for is

yourCompletable
    .test()
    .assertErrorMessage("your error message")
like image 129
Marc Calandro Avatar answered Oct 19 '25 07:10

Marc Calandro


There is an assertError for that, most cases use the version that takes the Thorwable's type as a parameter, from the docs:

Asserts that this TestObserver/TestSubscriber received exactly one onError event which is an instance of the specified errorClass class.

Usage:

yourCompletable
    .test()
    .assertError(RuntimeException::class.java)

Here you can find the three versions of assertError.

like image 27
Ahmed Ashraf Avatar answered Oct 19 '25 07:10

Ahmed Ashraf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!