作者:手机用户2502855477 | 来源:互联网 | 2023-09-11 19:29
Description
Hello. I constructed a simple JUnit test which tries to get a string from strings.xml (just to see if Robolectric works for my project) like this:
1 2 3 4 5 6 7 8 9 10
| java
(RobolectricTestRunner.class)
public class RoboStringTest {
public void getStringFromResources() {
Context context = RuntimeEnvironment.systemContext;
String actual = context.getString(R.string.error);
assertEquals("Error", actual);
}
} |
But it throws an exception:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| No package ID 7f found for ID 0x7f0e005d.
android.content.res.Resources$NotFoundException: String resource ID #0x7f0e005d
at android.content.res.Resources.getText(Resources.java:348)
at android.content.res.Resources.getString(Resources.java:441)
at android.content.Context.getString(Context.java:578)
at com.cookedapps.android.dronescout.RoboStringTest.getStringFromResources(RoboStringTest.java:21)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:600)
at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:260)
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.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)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131) |
I read every Github issue or Stackoverflow question regarding this problem but all provided solutions did not work for me.
Disabling AAPT2 with
1
| android.enableAapt2=false |
resulted in the same exception but i think this should not be a problem with Robolectric 4.
What I find interesting is the message
1
| "No package ID 7f found for ID 0x7f0e005d." |
. This stays the same for ever string I try to get. I don't understand this message. Does it mean that strings.xml could not be found?
What I have tried
- Added
1
| testOptions.unitTests.includeAndroidResources = true |
to my
- Added
1
| android.enableUnitTestBinaryResources=true |
to my
- Checked my JUnit Run Config (I am on Linux), Working dir is set to
- Switched my project to AndroidX
Robolectric & Android Version
Robolectric: 4.0.2
Robolectric-Multidex: 4.0.2
Sdk: 28
BuildTools: 28.0.3
Gradle: 3.2.1
AS: 3.2.1
on Linux Mint 18
该提问来源于开源项目:robolectric/robolectric
Have you tried adding
1
| testImplementation 'androidx.test:core:1.0.0' |
to the build gradle and then using
1
| ApplicationProvider.getApplicationContext(); |
to get context. I was able to perform file operations with this