0

Description When I write a custom shadow class named ShadowFoo.java for testing Foo.java, in the origin Foo, there is a public final static member variable named FIELD_A, some other class just invoke the FIELD_A directly by Foo.FIELD_A. And, there is some other method in Foo.java that the source which I tested is inoked, so I have to use shadow. I' ve tried much and can't find a way to resolve the member variable in shadow class, could someone know how to deal with it?

PS: The names of class and variable in my description is just for convenient to describe my problem. The real error info and source-code are copied from android studio as follow.

1.I copy the UserHandle.java into test directories, and delete other method only keep the member variable. When I run the test case, it worked correctly.

2.I wrote a same type and same name member variable in ShadowUserHandle.java, use @config(shadows = {ShadowUserHandle.class}) to deploy the shadow class, and it shows the error info.

java.lang.ExceptionInInitializerError Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:186) at org.robolectric.internal.bytecode.RobolectricInternals.classInitializing(RobolectricInternals.java:21) at com.oplus.wrapper.os.UserHandle.<clinit>(Unknown Source) at com.oplus.aod.cache.db.AodDBHelper.getInstance(AodDBHelper.java:22) at com.oplus.aod.cache.db.AodDBManager.<init>(AodDBManager.java:34) at com.oplus.aod.AodApplication.onCreate(AodApplication.kt:31)

Also, I've tried to use reflection with org.javassist, but javassist only provide addField() and remove() for custom fields type, and do not support setValue() for fields.

Is there any way to deal this problem with shaodow?

This is the error line of AodDBHelper.java

public static AodDBHelper getInstance(Context context) {
        USER_ID = UserHandle.USER_SYSTEM;           //The UserHandle is not android.os.UserHandle, we do some override, and it is the Foo I memtioned
        if (sDBHelper == null) {
            sDBHelper = new AodDBHelper(context);
        }
        return sDBHelper;
}

In the first trial, I Copied the UserHandle.java and worked correctly

public class UserHandle {
    public static final int USER_SYSTEM = 0;         //just the one line of source code
}

In the second trial, I delete the UserHandle.java in test directories, write a ShadowUserHandle.java and runs error, and the following is ShadowUserHandle.java

@Implements(UserHandle.class)
public class ShadowUserHandle {
    public static final int USER_SYSTEM = 0;
    @Implementation
    ....       //other methods which do not effect the error test case
}
wxhhh729
  • 41
  • 1
  • 4

0 Answers0