无法在包[android,org.robolectric.default]中找到资源ID#0x7f09001c

问题描述 投票:1回答:2

我正在使用Robolectric进行一些测试,但我遇到了一个我无法解决的问题。当我运行测试时,出现以下错误:

android.content.res.Resources $ NotFoundException:无法在包[android,org.robolectric.default]中找到资源ID#0x7f09001c

at org.robolectric.shadows.ShadowAssetManager.getResName(ShadowAssetManager.java:925)

在org.robolectric.shadows.ShadowAssetManager.loadXmlResourceParser(ShadowAssetManager.java:439)

在org.robolectric.shadows.ShadowResources.loadXmlResourceParser(ShadowResources.java:221)

在android.content.res.Resources.loadXmlResourceParser(Resources.java)

在android.content.res.Resources.getLayout(Resources.java:852)

在android.view.LayoutInflater.inflate(LayoutInflater.java:394)

在android.view.LayoutInflater.inflate(LayoutInflater.java:352)

在com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)

在android.app.Activity.setContentView(Activity.java:1867)

在com.example.robertoassad.roboletrictest.MainActivity.onCreate(MainActivity.java:15)

在android.app.Activity.performCreate(Activity.java:5008)

在org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:232)

在org.robolectric.android.controller.ActivityController $ 1.run(ActivityController.java:58)

在org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:364)

在org.robolectric.shadows.CoreShadowsAdapter $ 1.runPaused(CoreShadowsAdapter.java:26)

在org.robolectric.android.controller.ActivityController.create(ActivityController.java:55)

在org.robolectric.android.controller.ActivityController.create(ActivityController.java:65)

在org.robolectric.android.controller.ActivityController.setup(ActivityController.java:157)

在org.robolectric.Robolectric.setupActivity(Robolectric.java:101)

at com.example.robertoassad.roboletrictest.MainActivityTest.clickingLogin_shouldStartLoginActivity(MainActivityTest.java:20)

在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)

在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

在org.robolectric.RobolectricTestRunner $ HelperTestRunner $ 1.evaluate(RobolectricTestRunner.java:523)

在org.robolectric.internal.SandboxTestRunner $ 2.evaluate(SandboxTestRunner.java:226)

在org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:108)

在org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:35)

在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)

在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)

在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)

在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)

在org.robolectric.internal.SandboxTestRunner $ 1.evaluate(SandboxTestRunner.java:62)

在org.junit.runners.ParentRunner.run(ParentRunner.java:363)

在org.junit.runner.JUnitCore.run(JUnitCore.java:137)

在com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)

at com.intellij.rt.execution.junit.IdeaTestRunner $ Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)

在com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)

在com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

在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);

对我来说,我在这个问题上看不出来......

细节:

测试类位于文件夹: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());
    }
}
android unit-testing android-activity junit robolectric
2个回答
4
投票

这对我有用:

去了Robolectric Setup page并跟随他们的导游转到他们的最新版本。

换一种说法,

  • 添加了截至2017年2月17日的最新版本:testImplementation "org.robolectric:robolectric:3.7.1"
  • 在我的includeAndroidResources = true中添加了build.graddle

例如,

android {
  testOptions {
    unitTests {
      includeAndroidResources = true
    }
  }
}
dependencies {
    testImplementation "org.robolectric:robolectric:3.7.1"
}

在没有任何Config注释的情况下运行我的测试

@RunWith(RobolectricTestRunner.class)
public class MyTests {

    @Test
    public void singleTest() {
        //Do and verify or assert something
    }
}

0
投票

您需要在@Config中指定清单或常量,以便Robolectric知道在哪里找到资源。 Config.NONE无法正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.