Skip to content

AndroidSchedulers.mainThread() has Android dependencies in RxAndroid 2.0.0-RC2 #332

@peter-tackage

Description

@peter-tackage

I've just started experimenting with RxJava/RxAndroid 2.0.0-RC2 on an Android app and when attempting to write a (JVM) unit test which replaces AndroidSchedulers.mainThread() with a Scheduler defined via RxAndroidPlugins it seems that this is not possible as it requires Android Handler and Looper dependencies.

This is the Exception that is reported when I attempt to execute the unit tests:

java.lang.ExceptionInInitializerError
    at <blah, blah>
Caused by: java.lang.RuntimeException: Method getMainLooper in android.os.Looper not mocked. See http://g.co/androidstudio/not-mocked for details.
    at android.os.Looper.getMainLooper(Looper.java)
    at io.reactivex.android.schedulers.AndroidSchedulers.<clinit>(AndroidSchedulers.java:24)
    ... 32 more

And this is the code in question from AndroidSchedulers in RxAndroid 2.0.0-RC2:

    // snip
    private static final Scheduler MAIN_THREAD = RxAndroidPlugins.initMainThreadScheduler(
        new HandlerScheduler(new Handler(Looper.getMainLooper())));

    /** A {@link Scheduler} which executes actions on the Android main thread. */
    public static Scheduler mainThread() {
        return RxAndroidPlugins.onMainThreadScheduler(MAIN_THREAD);
    }
    //snip 

Looking at the previous AndroidSchedulers definition from Rx 1.x, this was not the case as it would return the plugin's instance without attempting to evaluate the statement with Android dependencies on demand: the RxAndroidPlugins instance would take preference.

So a few questions. Is my evaluation correct or am I missing something? Secondly, is this the intended behavior?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions