Melad Basilius
Melad Basilius

Reputation: 4306

Rx onErrorReturn is not called when test case run

I have

public Single<APIResponse> getPayment(Identity identity, String id) {
    return client.getPayment(identity, id).onErrorReturn(__ -> getTheSameEntityIfError(id));
}

client is a web-service client which call external service In case of any error returned from client.getPayment, then it will call getTheSameEntityIfError(id)

I am trying to write a test case to cover this method

@RunWith(MockitoJUnitRunner.class)
public class AdapterTest {

    @Mock
    PaymentsClient client;

    @InjectMocks
    Adapter adapter;

    Identity identity = testIdentity();
    PaymentEntity payment = testPayment();

    @Test
    public void getPayment() {
        when(client.getPayment(any(), any())).thenThrow(new NotFoundException());
        APIResponse apiResponse = adapter.getPayment(identity, "id").blockingGet();
        assertThat(payment.getId(), equalTo(apiResponse.getId(1)));
    }
}

I this case i want to simulate if the external service client.getPayment() returns an error, then call getTheSameEntityIfError(id). I expect the onErrorReturn operation will be called, but it always throw NotFoundException and the onErrorReturn never called.

Any idea what is wrong ?

Upvotes: 0

Views: 706

Answers (1)

akarnokd
akarnokd

Reputation: 69997

You mock client.getPayment() to throw instead of returning something RxJava can work with, thus RxJava doesn't even get involved in this case. Make client.getPayment return a Single.error().

Upvotes: 1

Related Questions