androidjunitwindows-10robolectric

LinkageError and IllegalAccessException exception thrown when I add shadow class in robolectric unit test


I use Android Studio, JUnit and Robolectric to test my codes. But there an exception thrown when I add shadow class into a test funciton.

Same code work successful on other computers (Windows and Linux).

Environment:

build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.tempapp"
        minSdkVersion 20
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility = 1.7
        targetCompatibility = 1.7
    }

    testOptions {
        unitTests.includeAndroidResources = true
        unitTests.all {
            systemProperty 'rebolectric.logging.enabled', 'true'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    testImplementation 'junit:junit:4.12'
    testImplementation "org.robolectric:robolectric:3.1"
}

FooTest.java (Can work successful):

@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE, constants = BuildConfig.class, sdk = 22)
public class FooTest {

    @Test
    public void a() {
        assertEquals(3, 1 + 2);
    }

    @Test
    public void b() {
        assertEquals(3, 1 + 2);
    }
}

Run above unit tests, succeed. But there is an exception thrown when I add a shadow class into a unit test, Just a @Config(shadows = {...})

FooTest.java (Exception when unit test run):

@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE, constants = BuildConfig.class, sdk = 22)
public class FooTest {

    @Test
    public void a() {
        assertEquals(3, 1 + 2);
    }

    @Test
    @Config(shadows = ShadowTextUtils.class) // Add a shadow
    public void b() {
        assertEquals(3, 1 + 2);
    }

    @Implements(value = TextUtils.class)
    public static class ShadowTextUtils { }
}

Exception:

java.lang.AssertionError: java.lang.IllegalAccessException: no such field: org.robolectric.internal.ProxyMaker$GeneratedProxy/1585841343.__proxy__/android.content.res.Configuration/putField

    at org.robolectric.internal.ProxyMaker.createProxyFactory(ProxyMaker.java:100)
    at org.robolectric.internal.ProxyMaker$1.computeValue(ProxyMaker.java:45)
    at org.robolectric.internal.ProxyMaker$1.computeValue(ProxyMaker.java:43)
    at java.lang.ClassValue.getFromHashMap(ClassValue.java:227)
    at java.lang.ClassValue.getFromBackup(ClassValue.java:209)
    at java.lang.ClassValue.get(ClassValue.java:115)
    at org.robolectric.internal.ProxyMaker.createProxy(ProxyMaker.java:51)
    at org.robolectric.internal.Shadow.directlyOn(Shadow.java:37)
    at org.robolectric.shadows.ShadowConfiguration.setToDefaults(ShadowConfiguration.java:149)
    at android.content.res.Configuration.setToDefaults(Configuration.java)
    at android.content.res.Configuration.__constructor__(Configuration.java:612)
    at android.content.res.Configuration.<init>(Configuration.java)
    at android.content.res.Configuration.__staticInitializer__(Configuration.java:48)
    at org.robolectric.util.ReflectionHelpers.callStaticMethod(ReflectionHelpers.java:226)
    at org.robolectric.internal.bytecode.RobolectricInternals.performStaticInitialization(RobolectricInternals.java:54)
    at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:114)
    at org.robolectric.internal.bytecode.RobolectricInternals.classInitializing(RobolectricInternals.java:18)
    at android.content.res.Configuration.<clinit>(Configuration.java)
    at org.robolectric.shadows.ShadowResources.getSystem(ShadowResources.java:96)
    at android.content.res.Resources.getSystem(Resources.java)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:106)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:423)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:254)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:191)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:56)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:157)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.IllegalAccessException: no such field: org.robolectric.internal.ProxyMaker$GeneratedProxy/1585841343.__proxy__/android.content.res.Configuration/putField
    at java.lang.invoke.MemberName.makeAccessException(MemberName.java:867)
    at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:1003)
    at java.lang.invoke.MethodHandles$Lookup.resolveOrFail(MethodHandles.java:1377)
    at java.lang.invoke.MethodHandles$Lookup.findSetter(MethodHandles.java:1048)
    at org.robolectric.internal.ProxyMaker.createProxyFactory(ProxyMaker.java:85)
    at org.robolectric.internal.ProxyMaker$1.computeValue(ProxyMaker.java:45)
    at org.robolectric.internal.ProxyMaker$1.computeValue(ProxyMaker.java:43)
    at java.lang.ClassValue.getFromHashMap(ClassValue.java:227)
    at java.lang.ClassValue.getFromBackup(ClassValue.java:209)
    at java.lang.ClassValue.get(ClassValue.java:115)
    at org.robolectric.internal.ProxyMaker.createProxy(ProxyMaker.java:51)
    at org.robolectric.internal.Shadow.directlyOn(Shadow.java:37)
    at org.robolectric.shadows.ShadowConfiguration.setToDefaults(ShadowConfiguration.java:149)
    at android.content.res.Configuration.setToDefaults(Configuration.java)
    at android.content.res.Configuration.$$robo$$__constructor__(Configuration.java:612)
    at android.content.res.Configuration.<init>(Configuration.java)
    at android.content.res.Configuration.__staticInitializer__(Configuration.java:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    ... 24 more
Caused by: java.lang.LinkageError: loader constraint violation: when resolving field "__proxy__" the class loader (instance of <bootloader>) of the referring class, org/robolectric/internal/ProxyMaker$GeneratedProxy, and the class loader (instance of org/robolectric/internal/bytecode/InstrumentingClassLoader) for the field's resolved type, android/content/res/Configuration, have different Class objects for that type
    at java.lang.invoke.MethodHandleNatives.resolve(Native Method)
    at java.lang.invoke.MemberName$Factory.resolve(MemberName.java:975)
    at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:1000)
    ... 43 more


Process finished with exit code -1

Solution

  • For everyone like me, who works with legacy codebase: your goal is to define system property robolectric.invokedynamic.enable=false. https://github.com/robolectric/robolectric/blob/571c28bf97632b4b91a87fa729be0c6a5150a714/sandbox/src/main/java/org/robolectric/internal/bytecode/InvokeDynamic.java#L8

    I've had my custom TestRunner defined already, so did it in the following way:

    public class TestRunner extends RobolectricTestRunner {
    
      static {
        System.setProperty("robolectric.invokedynamic.enable", "false");
      }
    }
    

    Or Gradle variant:

       testOptions {
            unitTests.all {
                systemProperty 'robolectric.invokedynamic.enable', 'false'
            }
        }
    

    P.S.

    $ java -version
    java version "1.8.0_201"
    Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
    Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)