Skip to content

Instantly share code, notes, and snippets.

@mtsahakis
Created December 5, 2017 20:24
Show Gist options
  • Save mtsahakis/cce02d735758dda9d8b009ac7189aa59 to your computer and use it in GitHub Desktop.
Save mtsahakis/cce02d735758dda9d8b009ac7189aa59 to your computer and use it in GitHub Desktop.
Rule for junit testing so as to run RxJava code on the junit Thread of execution. Instantiate as field member on a junit test class: @rule public TrampolineSchedulerRule mTrampolineSchedulerRule = new TrampolineSchedulerRule();
@Override
public Statement apply(final Statement base, Description d) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
RxJavaPlugins.setIoSchedulerHandler(scheduler -> Schedulers.trampoline());
RxJavaPlugins.setComputationSchedulerHandler(scheduler -> Schedulers.trampoline());
RxJavaPlugins.setNewThreadSchedulerHandler(scheduler -> Schedulers.trampoline());
RxAndroidPlugins.setInitMainThreadSchedulerHandler(schedulerCallable -> Schedulers.trampoline());
try {
base.evaluate();
} finally {
RxJavaPlugins.reset();
RxAndroidPlugins.reset();
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment