无法在包 [android、org.robolectric.default] 中找到资源 ID #0x7f09001c
Unable to find resource ID #0x7f09001c in packages [android, org.robolectric.default]
我正在 运行使用 Roboletric 进行一些测试,但我遇到了一个我无法解决的问题。我在运行测试的时候,出现如下错误:
android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f09001c in packages [android, org.robolectric.default]
at org.robolectric.shadows.ShadowAssetManager.getResName(ShadowAssetManager.java:925)
at org.robolectric.shadows.ShadowAssetManager.loadXmlResourceParser(ShadowAssetManager.java:439)
at org.robolectric.shadows.ShadowResources.loadXmlResourceParser(ShadowResources.java:221)
at android.content.res.Resources.loadXmlResourceParser(Resources.java)
at android.content.res.Resources.getLayout(Resources.java:852)
at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
at android.app.Activity.setContentView(Activity.java:1867)
at com.example.robertoassad.roboletrictest.MainActivity.onCreate(MainActivity.java:15)
at android.app.Activity.performCreate(Activity.java:5008)
at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:232)
at org.robolectric.android.controller.ActivityController.run(ActivityController.java:58)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:364)
at org.robolectric.shadows.CoreShadowsAdapter.runPaused(CoreShadowsAdapter.java:26)
at org.robolectric.android.controller.ActivityController.create(ActivityController.java:55)
at org.robolectric.android.controller.ActivityController.create(ActivityController.java:65)
at org.robolectric.android.controller.ActivityController.setup(ActivityController.java:157)
at org.robolectric.Robolectric.setupActivity(Robolectric.java:101)
at com.example.robertoassad.roboletrictest.MainActivityTest.clickingLogin_shouldStartLoginActivity(MainActivityTest.java:20)
at org.junit.runners.model.FrameworkMethod.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.evaluate(RobolectricTestRunner.java:523)
at org.robolectric.internal.SandboxTestRunner.evaluate(SandboxTestRunner.java:226)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:108)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:35)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=17=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.robolectric.internal.SandboxTestRunner.evaluate(SandboxTestRunner.java:62)
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:51)
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)
在
com.example.robertoassad.alltestsmerge.MainActivity.onCreate(MainActivity.java:15)
在 MainActivity.java:15 上有以下代码的错误:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
具体在:setContentView(R.layout.activity_main);
以及
com.example.robertoassad.roboletrictest.MainActivityTest.clickingLogin_shouldStartLoginActivity(MainActivityTest.java:20)
在 MainActivityTest.java:20 上,错误具体在:
MainActivity activity = Robolectric.setupActivity(MainActivity.class);
对我来说,我认为这个问题没有意义...
详情:
测试 class 在文件夹中:app\src\test\java\{package}
测试是:
@RunWith(RobolectricTestRunner.class)
@Config(manifest= Config.NONE)
public class MainActivityTest {
@Test
public void clickingLogin_shouldStartLoginActivity() {
MainActivity activity = Robolectric.setupActivity(MainActivity.class);
activity.findViewById(R.id.login).performClick();
Intent expectedIntent = new Intent(activity, HomeActivity.class);
Intent actual = ShadowApplication.getInstance().getNextStartedActivity();
assertEquals(expectedIntent.getComponent(), actual.getComponent());
}
}
您需要在@Config 中指定清单或常量,以便Robolectric 知道在哪里可以找到资源。 Config.NONE 不会工作。
这对我有用:
前往 Robolectric Setup page 并按照他们的指南移动到最新版本。
换句话说,
- 添加了截至 2017 年 2 月 17 日的最新版本:
testImplementation "org.robolectric:robolectric:3.7.1"
- 在我的
build.graddle
中添加了 includeAndroidResources = true
例如,
android {
testOptions {
unitTests {
includeAndroidResources = true
}
}
}
dependencies {
testImplementation "org.robolectric:robolectric:3.7.1"
}
运行 我的测试没有任何配置注释
@RunWith(RobolectricTestRunner.class)
public class MyTests {
@Test
public void singleTest() {
//Do and verify or assert something
}
}
我正在 运行使用 Roboletric 进行一些测试,但我遇到了一个我无法解决的问题。我在运行测试的时候,出现如下错误:
android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f09001c in packages [android, org.robolectric.default]
at org.robolectric.shadows.ShadowAssetManager.getResName(ShadowAssetManager.java:925)
at org.robolectric.shadows.ShadowAssetManager.loadXmlResourceParser(ShadowAssetManager.java:439)
at org.robolectric.shadows.ShadowResources.loadXmlResourceParser(ShadowResources.java:221)
at android.content.res.Resources.loadXmlResourceParser(Resources.java)
at android.content.res.Resources.getLayout(Resources.java:852)
at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
at android.app.Activity.setContentView(Activity.java:1867)
at com.example.robertoassad.roboletrictest.MainActivity.onCreate(MainActivity.java:15)
at android.app.Activity.performCreate(Activity.java:5008)
at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:232)
at org.robolectric.android.controller.ActivityController.run(ActivityController.java:58)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:364)
at org.robolectric.shadows.CoreShadowsAdapter.runPaused(CoreShadowsAdapter.java:26)
at org.robolectric.android.controller.ActivityController.create(ActivityController.java:55)
at org.robolectric.android.controller.ActivityController.create(ActivityController.java:65)
at org.robolectric.android.controller.ActivityController.setup(ActivityController.java:157)
at org.robolectric.Robolectric.setupActivity(Robolectric.java:101)
at com.example.robertoassad.roboletrictest.MainActivityTest.clickingLogin_shouldStartLoginActivity(MainActivityTest.java:20)
at org.junit.runners.model.FrameworkMethod.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.evaluate(RobolectricTestRunner.java:523)
at org.robolectric.internal.SandboxTestRunner.evaluate(SandboxTestRunner.java:226)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:108)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:35)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=17=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.robolectric.internal.SandboxTestRunner.evaluate(SandboxTestRunner.java:62)
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:51)
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)
在
com.example.robertoassad.alltestsmerge.MainActivity.onCreate(MainActivity.java:15)
在 MainActivity.java:15 上有以下代码的错误:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
具体在:setContentView(R.layout.activity_main);
以及
com.example.robertoassad.roboletrictest.MainActivityTest.clickingLogin_shouldStartLoginActivity(MainActivityTest.java:20)
在 MainActivityTest.java:20 上,错误具体在:
MainActivity activity = Robolectric.setupActivity(MainActivity.class);
对我来说,我认为这个问题没有意义...
详情:
测试 class 在文件夹中:app\src\test\java\{package}
测试是:
@RunWith(RobolectricTestRunner.class)
@Config(manifest= Config.NONE)
public class MainActivityTest {
@Test
public void clickingLogin_shouldStartLoginActivity() {
MainActivity activity = Robolectric.setupActivity(MainActivity.class);
activity.findViewById(R.id.login).performClick();
Intent expectedIntent = new Intent(activity, HomeActivity.class);
Intent actual = ShadowApplication.getInstance().getNextStartedActivity();
assertEquals(expectedIntent.getComponent(), actual.getComponent());
}
}
您需要在@Config 中指定清单或常量,以便Robolectric 知道在哪里可以找到资源。 Config.NONE 不会工作。
这对我有用:
前往 Robolectric Setup page 并按照他们的指南移动到最新版本。
换句话说,
- 添加了截至 2017 年 2 月 17 日的最新版本:
testImplementation "org.robolectric:robolectric:3.7.1"
- 在我的
build.graddle
中添加了
includeAndroidResources = true
例如,
android {
testOptions {
unitTests {
includeAndroidResources = true
}
}
}
dependencies {
testImplementation "org.robolectric:robolectric:3.7.1"
}
运行 我的测试没有任何配置注释
@RunWith(RobolectricTestRunner.class)
public class MyTests {
@Test
public void singleTest() {
//Do and verify or assert something
}
}