I configured Robolectric as on their website:
android {
testOptions {
unitTests {
includeAndroidResources = true
}
}
}
dependencies {
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.robolectric:robolectric:4.12.2'
}
And created my first test:
@RunWith(RobolectricTestRunner.class)
public class MyTest {
@Test
public void testMethod() {
try(ActivityController<MyActivity> controller = Robolectric.buildActivity(MyActivity.class)) {
controller.setup();
}
}
}
However, when running the test, it gives an error at buildActivity
:
java.lang.RuntimeException: Method getMainLooper in android.os.Looper not mocked. See https://developer.android.com/r/studio-ui/build/not-mocked for details.
The corresponding site says that adding returnDefaultValues = true
fixes it:
android {
...
testOptions {
unitTests.returnDefaultValues = true
}
And that’s also true. But then I get another error:
java.lang.NullPointerException: Cannot invoke “android.os.Looper.getThread()” because the return value of “android.os.Looper.getMainLooper()” is null
So I think there is a more deeper problem as to why Robolectric doesn’t work. The error makes sense, as probably the default return value is null
. But I have no other idea on how to fix it.
Does anybody know as to why Robolectric doesn’t run?