Created
December 12, 2016 15:20
-
-
Save macsystems/4dab18dc363a4859e2e39215223a7b37 to your computer and use it in GitHub Desktop.
Custom JUnit Test Rule which allows to override the Main Thread and oder Threads used by RxJava
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
import org.junit.rules.TestRule; | |
import org.junit.runner.Description; | |
import org.junit.runners.model.Statement; | |
import io.reactivex.android.plugins.RxAndroidPlugins; | |
import io.reactivex.plugins.RxJavaPlugins; | |
import io.reactivex.schedulers.Schedulers; | |
public class RxJavaRule implements TestRule { | |
@Override | |
public Statement apply(Statement base, Description description) { | |
return new Statement() { | |
@Override | |
public void evaluate() throws Throwable { | |
try { | |
RxAndroidPlugins.setInitMainThreadSchedulerHandler(e -> Schedulers.trampoline()); | |
RxJavaPlugins.setIoSchedulerHandler(e -> Schedulers.trampoline()); | |
RxJavaPlugins.setInitComputationSchedulerHandler(e -> Schedulers.trampoline()); | |
RxJavaPlugins.setInitNewThreadSchedulerHandler(e -> Schedulers.trampoline()); | |
RxJavaPlugins.setInitSingleSchedulerHandler(e -> Schedulers.trampoline()); | |
} catch (Exception e) { | |
throw new RuntimeException("Failed to apply RxSchedulers", e); | |
} | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment