【问题标题】:Unable to find resource ID #0x7f09001c in packages [android, org.robolectric.default]在包 [android, org.robolectric.default] 中找不到资源 ID #0x7f09001c
【发布时间】:2017-11-28 11:44:34
【问题描述】:

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

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

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

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

在 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


    【解决方案1】:

    这对我有用:

    转到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
        }
    }
    

    【讨论】:

      【解决方案2】:

      您需要在 @Config 中指定清单或常量,以便 Robolectric 知道在哪里可以找到资源。 Config.NONE 不起作用。

      猜你喜欢
      • 2021-05-22
      • 1970-01-01
      • 2015-04-13
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多