After updating my Android project to AGP 3.4.0, Robolectric to 4.2, and converting to AndroidX I am encountering issues running unit tests with AndroidJUnit4.class. Any tests run involving Robolectric throw errors related to shadows. One example is
java.lang.NoClassDefFoundError: Could not initialize class org.robolectric.shadows.ShadowUsageStatsManager
at org.robolectric.Shadows.reset(Shadows.java:2118)
at org.robolectric.RobolectricTestRunner.lambda$finallyAfterTest$0(RobolectricTestRunner.java:333)
at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:75)
at org.robolectric.RobolectricTestRunner.finallyAfterTest(RobolectricTestRunner.java:329)
at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:265)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
at java.util.concurrent.FutureTask.run(FutureTask.java)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Looking into the ShadowUsageStatsManager (and similar classes) it seems that the imports still include support library imports (such as import android.annotation.NonNull). I am unable to figure out what is causing this as my Robolectric dependency is updated to 4.3 and I have made the full AndroidX conversion process.
Related build.gradle dependencies include:
testImplementation 'junit:junit:4.12'
testImplementation 'androidx.test:runner:1.1.1'
testImplementation 'androidx.test:rules:1.1.1'
testImplementation 'androidx.test.ext:junit:1.1.0'
testImplementation 'org.robolectric:robolectric:4.2'
testImplementation 'org.robolectric:shadows-supportv4:4.2'
testImplementation 'org.robolectric:shadows-multidex:4.2'
testImplementation 'org.robolectric:shadows-httpclient:4.2'
In the build.gradle I also have includedAndroidResources and am targeting compileSdkVersion 28
testOptions {
unitTests.returnDefaultValues = true
unitTests.includeAndroidResources = true
unitTests {
all {
jvmArgs '-noverify'
}
}
}
And I have enabled unitTestBinaryResources in gradle.properties
android.enableUnitTestBinaryResources=true
I also followed the robolectric migration guide replacing deprecate methods, even running empty tests fail if they are run using the RobolectricTestRunner. Any insight as to why unit tests are failing is much appreciated, thank you.