Skip to content

Instantly share code, notes, and snippets.

@macsystems
Created December 12, 2016 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macsystems/4dab18dc363a4859e2e39215223a7b37 to your computer and use it in GitHub Desktop.
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
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