New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Robolectric doesn't support @UiThreadTest
#9026
Comments
cc @brettchabot Are you using RobolectricTestRunner or AndroidJUnit4? Also, which annotation are you referring to? Is it this one: Or https://developer.android.com/reference/android/support/test/annotation/UiThreadTest? Or the AndroidX one? This annotation seems very specific to instrumentation tests, which does not really apply to RobolectricTestRunner. In Robolectric, test methods run on the UI thread by default, so supporting this annotation may be redundant. if your tests need some specific behavior when |
I am using AndroidJUnit4 which has been modified to automatically switch to RobolectricTestRunner based on the test environment. Also I am using Based on the recommended pattern for sharedTest, the behavior should be same on both environments (android and robolectric)
this issue is specific to when using |
Unfortunately this is a known issue. Robolectric doesn't support UiThreadTest. As a workaround, would posting the work to the main Looper be a workaround? eg
|
The above snipped doesn't work for @before and @after blocks. I was able to workaround it temporarily by defining a custom TestRule similar to UiThreadStatement and using some reflection code to figure out if the rest is running on android or in robolectric. Though Ideally it should be implemented in the RobolectricTestRunner itself similar to AndroidJUnit4ClassRunner otherwise it breaks the sharedTest pattern |
When migrating a test which was using AndroidJUnit4 to robolectric with looperMode = INSTRUMENTATION_TEST,
@UiThreadTest
annotations are ignored and the test runs doesn't run on the main threadThe text was updated successfully, but these errors were encountered: