Reputation: 900
I'm trying to debug the inner success callback of an RxJava Subscription object and verify certain methods inside that get called with the correct argument(s). I read up some about Captors and though maybe that's the way to go but was unable to find an example that matched my needs.
I'm unit testing with: Mockito and JUnit
// OrderHistoryPresenterImpl presenter;
public void loadOrderHistory(final int offset, final int limit) {
mCompositeSubscription.add(useCase.getOrderHistory(offset, limit)
.doOnError(throwable -> Timber.e(throwable, "Error"))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<ListOfOrders>() {
@Override
public void onCompleted() {}
@Override
public void onError(Throwable e) {}
@Override
public void onNext(ListOfOrders listOfOrders) {
//
// I would like to verify the calls in this method
//
user.setTryoutCountActive(listOfOrders.getActiveTryoutsCount());
getView().addOrdersToView(listOfOrders);
mCompositeSubscription.clear();
}
})
);
}
And here are the tests I'm trying to test it with
@RunWith(MockitoJUnitRunner.class)
public class OrderHistoryPresenterTest {
// Boiler plate Dagger 2 setup + variable declarations
private ListOfOrders response;
private ListOfOrders spyListOfOrders;
private OrderHistoryPresenterImpl presenter;
@Mock private OrderHistoryUseCaseImpl useCase;
@Before
public void setUp() throws FileNotFoundException {
// Setup + dagger 2 setup…
response = Utils.fromJson(gson, this, "OrderHistory.json", type);
spyListOfOrders = spy(response);
presenter = new OrderHistoryPresenterImpl(app, useCase, user);
when(useCase.getOrderHistory(MOCK_USER_ACCESS_TOKEN)).thenReturn(Observable.just(spyListOfOrders));
}
@After
public void tearDown() {…}
@Test
public void shouldGetOrderAndCall_addOrdersToView_OnActivity() {
presenter.loadOrderHistory(MOCK_OFFSET, MOCK_LIMIT);
// works…
verify(useCase, times(1)).getOrderHistory(MOCK_USER_ACCESS_TOKEN);
// This fails because it gets called right away because it's async - what to do?
verify(view, times(1)).addOrdersToView(spyListOfOrders);
}
}
So as you can see I don't know how to test the inner Subscriber<ListOfOrders>
callback methods. Any help would be greatly appreciated!
Upvotes: 0
Views: 1449
Reputation: 1127
Let's say if you rewrite your method like this,
// OrderHistoryPresenterImpl presenter;
public void loadOrderHistory(final int offset, final int limit) {
mCompositeSubscription.add(useCase.getOrderHistory(offset, limit)
.doOnError(throwable -> Timber.e(throwable, "Error"))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<ListOfOrders>() {
@Override
public void onCompleted() {}
@Override
public void onError(Throwable e) {}
@Override
public void onNext(ListOfOrders listOfOrders) {
load(listOfOrders)
}
})
);
}
void loadOrders(ListOfOrders list) {
// I would like to verify the calls in this method
user.setTryoutCountActive(listOfOrders.getActiveTryoutsCount());
getView().addOrdersToView(listOfOrders);
mCompositeSubscription.clear();
}
Now test this loadOrders
method separately. Because all it cares about is getting a list of orders to process. It can come from anywhere (not only observable).
You can unit test this method now.
If you want to test Observable, then use TestSubscriber
to see if you are actually getting a list of orders from Observable.
Ref : https://reactivex.io/RxJava/javadoc/rx/observers/TestSubscriber.html
Upvotes: 1