Created
December 5, 2017 20:24
-
-
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();
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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