Skip to content
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

Method getLocales in android.content.res.Configuration not mocked #4073

Closed
ScottPierce opened this issue Nov 2, 2018 · 16 comments
Closed

Method getLocales in android.content.res.Configuration not mocked #4073

ScottPierce opened this issue Nov 2, 2018 · 16 comments
Labels
Milestone

Comments

@ScottPierce
Copy link

ScottPierce commented Nov 2, 2018

Description

We see this intermittently on our linux Jenkins environment. We're seeing this on roughly 40% of our builds, but the code is private, and it's also pretty massive, so I can't give you a repro of it. I noticed someone else was encountering this as well: #4015

Switching to sdk 28 didn't fix this for us.

Error
java.lang.RuntimeException: Method getLocales in android.content.res.Configuration not mocked. See http://g.co/androidstudio/not-mocked for details.
Stacktrace
java.lang.RuntimeException: Method getLocales in android.content.res.Configuration not mocked. See http://g.co/androidstudio/not-mocked for details.
at android.content.res.Configuration.getLocales(Configuration.java)
at org.robolectric.android.DeviceConfig.getLocale(DeviceConfig.java:397)
at org.robolectric.android.DeviceConfig.applyRules(DeviceConfig.java:244)
at org.robolectric.android.Bootstrap.applyQualifiers(Bootstrap.java:50)
at org.robolectric.android.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:106)
at org.robolectric.RobolectricTestRunner.beforeTest(RobolectricTestRunner.java:377)
at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:252)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:130)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:42)
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.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:84)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:106)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)
at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:66)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:117)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:155)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:137)
at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
at java.lang.Thread.run(Thread.java:748)
Standard Output
[Robolectric] com.example.qualification.component.QualConditionsComponentTest.isDiabetesSelectedTest: sdk=28; resources=binary
[Robolectric] com.example.qualification.component.QualConditionsComponentTest.continueTest: sdk=28; resources=binary
[Robolectric] com.example.qualification.component.QualConditionsComponentTest.defaultStateTest: sdk=28; resources=binary
[Robolectric] com.example.qualification.component.QualConditionsComponentTest.isHighCholesterolSelectedTest: sdk=28; resources=binary
[Robolectric] com.example.qualification.component.QualConditionsComponentTest.isPreDiabetesSelectedTest: sdk=28; resources=binary
[Robolectric] com.example.qualification.component.QualConditionsComponentTest.isNoneSelectedTest: sdk=28; resources=binary
[Robolectric] com.example.qualification.component.QualConditionsComponentTest.isSmokingSelectedTest: sdk=28; resources=binary
[Robolectric] com.example.qualification.component.QualConditionsComponentTest.isHighBloodPressureSelectedTest: sdk=28; resources=binary
[Robolectric] com.example.qualification.component.QualConditionsComponentTest.backTest: sdk=28; resources=binary

Steps to Reproduce

Robolectric & Android Version

Android version 28
Robolectric 4.0.0

@xian xian added this to the 4.0.2 milestone Nov 2, 2018
@xian xian added the defect label Nov 2, 2018
@xian
Copy link
Member

xian commented Nov 2, 2018

Can you include the compileSdkVersion, targetSdkVersion, etc. from your build.gradle, and any SDK-related bits of your manifest?

Do you have android.testOptions.unitTests.returnDefaultValues configured in your build.gradle?

@ScottPierce
Copy link
Author

ScottPierce commented Nov 2, 2018

minSdkVersion: 21
targetSdkVersion: 28
compileSdkVersion: 28

This is our test options:

testOptions {
    execution 'ANDROID_TEST_ORCHESTRATOR'
    animationsDisabled true

    unitTests {
        includeAndroidResources = true
    }
}

I added returnDefaultValues. Will see if that fixes it.

@ScottPierce
Copy link
Author

Adding returnDefaultValues doesn't fix it, but it does change the error:

Error
java.lang.NullPointerException
Stacktrace
java.lang.NullPointerException
at org.robolectric.android.DeviceConfig.getLocale(DeviceConfig.java:397)
at org.robolectric.android.DeviceConfig.applyRules(DeviceConfig.java:244)
at org.robolectric.android.Bootstrap.applyQualifiers(Bootstrap.java:50)
at org.robolectric.android.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:106)
at org.robolectric.RobolectricTestRunner.beforeTest(RobolectricTestRunner.java:377)
at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:252)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:130)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:42)
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.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:84)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:106)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)
at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:66)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
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)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:117)
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)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:155)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:137)
at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
at java.lang.Thread.run(Thread.java:748)
Standard Output
[Robolectric] com.example.auth.component.ForgotPasswordComponentTest.defaultStateTest: sdk=28; resources=binary
[Robolectric] com.example.auth.component.ForgotPasswordComponentTest.emailUpdateTest: sdk=28; resources=binary
[Robolectric] com.example.auth.component.ForgotPasswordComponentTest.backTest: sdk=28; resources=binary

@ScottPierce
Copy link
Author

I'm also seeing this on occasion:

Error
java.lang.NoClassDefFoundError: android/content/pm/PackageManager$NameNotFoundException
Stacktrace
java.lang.NoClassDefFoundError: android/content/pm/PackageManager$NameNotFoundException
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
at java.lang.Class.getConstructor0(Class.java:3075)
at java.lang.Class.getConstructor(Class.java:1825)
at org.robolectric.RobolectricTestRunner.getHooksInterface(RobolectricTestRunner.java:552)
at org.robolectric.RobolectricTestRunner.configureSandbox(RobolectricTestRunner.java:233)
at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:230)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:130)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:42)
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.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:84)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:106)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)
at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:66)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
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)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:117)
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)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:155)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:137)
at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: couldn't load android.content.pm.PackageManager$NameNotFoundException
at org.robolectric.internal.bytecode.SandboxClassLoader.getByteCode(SandboxClassLoader.java:168)
at org.robolectric.internal.bytecode.SandboxClassLoader.maybeInstrumentClass(SandboxClassLoader.java:118)
at org.robolectric.internal.bytecode.SandboxClassLoader.lambda$findClass$0(SandboxClassLoader.java:111)
at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:50)
at org.robolectric.internal.bytecode.SandboxClassLoader.findClass(SandboxClassLoader.java:110)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 46 more
Caused by: java.util.zip.ZipException: invalid LOC header (bad signature)
at java.util.zip.ZipFile.read(Native Method)
at java.util.zip.ZipFile.access$1400(ZipFile.java:60)
at java.util.zip.ZipFile$ZipFileInputStream.read(ZipFile.java:734)
at java.util.zip.ZipFile$ZipFileInflaterInputStream.fill(ZipFile.java:434)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at org.robolectric.util.Util.copy(Util.java:21)
at org.robolectric.util.Util.readBytes(Util.java:38)
at org.robolectric.internal.bytecode.SandboxClassLoader.getByteCode(SandboxClassLoader.java:166)
... 52 more
Standard Output
[INFO] Unable to find resource 'org.robolectric:android-all:jar:9-robolectric-4913185-2' in repository sonatype (https://oss.sonatype.org/content/groups/public/)
Standard Error
Downloading: org/robolectric/android-all/9-robolectric-4913185-2/android-all-9-robolectric-4913185-2.pom from repository sonatype at https://oss.sonatype.org/content/groups/public/
Transferring 2K from sonatype
Downloading: org/robolectric/android-all/9-robolectric-4913185-2/android-all-9-robolectric-4913185-2.jar from repository sonatype at https://oss.sonatype.org/content/groups/public/
Transferring 118099K from sonatype
Downloading: org/robolectric/android-all/9-robolectric-4913185-2/android-all-9-robolectric-4913185-2.jar from repository central at http://repo1.maven.org/maven2

@wongk
Copy link

wongk commented Nov 6, 2018

We are seeing this issue on our build servers, but not 100%. I am not seeing it locally when run from Android Studio.

@ScottPierce
Copy link
Author

I'm not sure it will help, but I'll mention that I don't see this when running locally either.

My team is currently in the process of swapping to a new Jenkins environment, so we are running our CI jobs in 2 places atm. We have only seen this when running our tests inside of a docker container ( https://github.com/thyrlian/AndroidSDK ), inside of an aws instance. For this setup, everything is re-downloaded almost every run.

For our other jenkins environment where we haven't seen this yet, we re-use the previously cached maven dependencies, and android sdk.

@wongk
Copy link

wongk commented Nov 6, 2018

We are using Bamboo, which builds from a clean environment each time.

@OmarBeshary
Copy link

OmarBeshary commented Nov 7, 2018

I'm was facing the same error , I changed my targetSdkVersion from 28 to 27 and my test passed .
You can use @config(sdk = 27) above your test class as Robolectric runs on the default application target SDK if you don't want to change your target SDK . or simply change the application target SDK

@xian
Copy link
Member

xian commented Nov 9, 2018

The issue in #4073 (comment) is probably #2346, unrelated.

@xian xian modified the milestones: 4.0.2, 4.0.3 Nov 9, 2018
@jongerrish
Copy link
Contributor

@ScottPierce The classloader is loading android.content.res.Configuration.class from the mockable jar that studio is providing on the system classpath. I suspect its falling back on the mockable jar class definition because of some corruption in android-all-9-robolectric-4913185-2.jar

What is interesting is that it looks like the jar is being downloaded twice...

Downloading: org/robolectric/android-all/9-robolectric-4913185-2/android-all-9-robolectric-4913185-2.jar from repository sonatype at https://oss.sonatype.org/content/groups/public/
Transferring 118099K from sonatype
Downloading: org/robolectric/android-all/9-robolectric-4913185-2/android-all-9-robolectric-4913185-2.jar from repository central at http://repo1.maven.org/maven2

So I'm wondering if this is the source of the corruption.

The ant/maven code that fetches the dependencies is a source of a pain for a lot of users. Internally we store the jars locally (they very very rarely change).

If you'd like to investigate following this option you set

-Drobolectric.offline=true -Drobolectric.dependency.dir=/local/path/to/android-jars/

@ScottPierce
Copy link
Author

@jongerrish I'm considering creating a docker image with all our dependencies cached as a potential solution. Is there a way to ensure all the robolectric dependencies are downloaded without running the tests? I seem to recall that robolectric may download certain things at run time, and not compile time.

@ScottPierce
Copy link
Author

@jongerrish Can you provide more information for how to set this up locally? What files should I download? Just the jar? https://oss.sonatype.org/content/groups/public/org/robolectric/android-all/9-robolectric-4913185-2/

Also, am I able to set the following in my gradle?

robolectric.offline=true 
robolectric.dependency.dir=/local/path/to/android-jars/

I ran the command you suggested locally with a bad path, thinking that it'd fail, but it didn't.

@ScottPierce
Copy link
Author

@xian When will 4.0.3 be released?

@pecpwee
Copy link

pecpwee commented Aug 23, 2019

I met the same error on the latest version 4.3 . and I have found a way to work around:set the sdk to 23 by @config(manifest = Config.NONE, sdk = 23).
when version of sdk has been set 23,robolectric won‘t invoke Configuration.getLocales() method.

@tangxiangpi
Copy link

I met the same error on the latest version 4.3 . and I have found a way to work around:set the sdk to 23 by @config(manifest = Config.NONE, sdk = 23).
when version of sdk has been set 23,robolectric won‘t invoke Configuration.getLocales() method.
me too, all thing is fine in win10 and mac machines, but failed on a win10 server machine , just the same as you ,what is the problem ?

@tangxiangpi
Copy link

I met the same error on the latest version 4.3 . and I have found a way to work around:set the sdk to 23 by @config(manifest = Config.NONE, sdk = 23).
when version of sdk has been set 23,robolectric won‘t invoke Configuration.getLocales() method.
if (apiLevel >= VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(locale);
} else {
configuration.locale = locale;
}
there is a new failed,setLocales not mocked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants