I'm trying to add some unit tests to following piece of code:
public List<Stuff> extractStuff(Scheduler scheduler, List<Token> tokens) {
List<Observable<List<Stuff>>> observables = tokens
.stream()
.map(token-> Observable.just(getStuffByToken(token)).subscribeOn(scheduler))
.collect(Collectors.toList());
List<Stuff> result = new ArrayList<>();
for (List<Stuff> stuff: Observable.merge(observables).toBlocking().toIterable()) {
result.addAll(stuff);
}
return result;
}
I want Stuff objects to be fetched with parallelism, but I need to collect all of them before going any further (otherwise the next process does not make any sense).
Code works as expected, but I'm struggling with unit tests:
@Test
public void extractStuff() {
// GIVEN
TestScheduler scheduler = new TestScheduler();
List<Token> tokens = buildTokens();
...
// WHEN
List<Stuff> result = this.instance.extractStuff(scheduler, tokens);
// Execution never comes to this point...
// THEN
...
}
Using debugger, I can see that Observable.just(...) looks good (my list of observables is not empty, and I can see my custom mocked object inside).
Problem: the execution seems to be stuck at the Observable.merge(observables).toBlocking() expression. The line result.addAll is never called.
I have tried several things with the TestScheduler object, but I cannot manage to make it work. Most examples I've find across the Internet are dealing with a function that returns Observable object, so the associated unit test can run scheduler.advanceTimeBy(...);. In my situation, I can't apply this approach as Observable object are not returned directly.
Thanks a lot for your help!