How should I use RxJava\'s TestScheduler? I come from a .NET background but the TestScheduler in RxJava does not seem to work the same way as the t
you have some class:
public class SomeClass {
public void someMethod() {
Observable tick = Observable.interval(1, TimeUnit.SECONDS);
contactsRepository.find(index)
.buffer(MAX_CONTACTS_FETCH)
.zipWith(tick, new Func2, Long, List>() {
@Override
public List call(List contactList, Long aLong) {
return contactList;
}
}).subscribe()
}
}
Look up [Observable.interval][1] in the docs and you will see it operates on the computation scheduler, so lets override that in our test.
public class SomeClassTest {
private TestScheduler testScheduler;
@Before
public void before() {
testScheduler = new TestScheduler();
// set calls to Schedulers.computation() to use our test scheduler
RxJavaPlugins.setComputationSchedulerHandler(ignore -> testScheduler);
}
@After
public void after() {
// reset it
RxJavaPlugins.setComputationSchedulerHandler(null);
}
@Test
public void test() {
SomeClass someInstance = new SomeClass();
someInstance.someMethod();
// advance time manually
testScheduler.advanceBy(1, TimeUnit.SECONDS);
}
This solution is an improvement to the accepted answer as the quality, integrity and simplicity of the production code is maintained.