I am trying to test a presenter that uses RxJava to retrieve data from an interactor. In the setup method, I do something like:
@Before public void setup() { RxAndroidPlugins.getInstance().registerSchedulersHook(new RxAndroidSchedulersHook() { @Override public Scheduler getMainThreadScheduler() { return Schedulers.immediate(); } }); }
So, in my testing method, I can check the master call:
@Test public void testLoad() { presenter.load(); verify(view).dataLoaded(data); verify(interactor).load(); }
If I run the test with Android Studio, everything works as expected, the problem is that if I try on the command line
gradle test
Then the test will fail because: Actually, there were zero interactions with this mock.
So, I tried to put Thread.sleep (2000) right after calling the lead, and then it works, so I assume that Schedulers.immediate (); doesn't work from the command line, but I have no idea why and how to debug / fix. Do you have any ideas?
EDIT: Presenter Implementation →
public void load() { Observable<List<Data>> obs = interactor.load() .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()); obs.subscribe(new Observer<List<Data>>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(List<Data> data) { view.dataLoaded(data); } }); }
source share