How do I access the background stream in Robolectric?

I am testing the code in Robolectric runner. The code under test checks to see if it is running on the main thread:

    if (Looper.getMainLooper().getThread() == java.lang.Thread.currentThread()) {
        new IllegalStateException("Method called on the UI thread");
    }

      

Robolectric's test throws this exception and I don't want that. I tried to run the code from Robolectric.getBackgroundScheduler()

, but I have an exception.

How do I run my test on a different thread?

+3


source to share


1 answer


As suggested in the comments, validation can use a utility method:



static void checkMainThread() {
  if (isRunningInRobolectric()) {
    return;
  }
  if (Looper.getMainLooper().getThread() == java.lang.Thread.currentThread()) {
    new IllegalStateException("Method called on the UI thread");
  }
}

static boolean isRunningInRobolectric() {
  return "robolectric".equals(Build.FINGERPRINT);
}

      

0


source







All Articles