【问题标题】:Resources$NotFoundException when calling Robolectric.buildActivity()调用 Robolectric.buildActivity() 时的 Resources$NotFoundException
【发布时间】:2015-08-19 03:18:00
【问题描述】:

我正在使用 Robolectric 3.0 RC3 在 Android Studio 中进行单元测试。每次运行测试时我都会收到ResourceNotFoundException。请帮我解决问题。

build.gradle

dependencies {
    testCompile 'junit:junit:4.12'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.0.0'
    testCompile 'org.mockito:mockito-core:1.9.5'
    testCompile 'org.robolectric:robolectric:3.0-rc3'
    compile 'com.ibm.icu:icu4j:53.1'
}

测试类

@Before
public void setUp() throws Exception {
    activity = Robolectric.buildActivity(MainActivity.class).create().get();
}
@Test
public void pressingTheButtonShouldStartTheListActivity() throws Exception {
   //Something to test
}

所以,每当Robolectric.buildActivity() 我得到Resources$NotFoundException。资源#0x7f040016 指向R.layout.activity_main(xml)。跟踪 id 堆栈跟踪-

android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f040016
at org.robolectric.shadows.ShadowResources.checkResName(ShadowResources.java:343)
at org.robolectric.shadows.ShadowResources.resolveResName(ShadowResources.java:338)
at org.robolectric.shadows.ShadowResources.loadXmlResourceParser(ShadowResources.java:429)
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.aricent.anas.unittesting2.MainActivity.onCreate(MainActivity.java:15) //****HERE****
at android.app.Activity.performCreate(Activity.java:5008)
at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:195)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:122)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:305)
at org.robolectric.shadows.CoreShadowsAdapter$2.runPaused(CoreShadowsAdapter.java:45)
at org.robolectric.util.ActivityController.create(ActivityController.java:118)
at org.robolectric.util.ActivityController.create(ActivityController.java:129)
at com.aricent.anas.unittesting2.MainActivityTest.setUp(MainActivityTest.java:32) //****HERE****
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.RunBefores.evaluate(RunBefores.java:24)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:245)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:185)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
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.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:149)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)


Process finished with exit code -1

MainActivity.java:15setContentView(R.layout.activity_main);

MainActivityTest.java:32activity = Robolectric.buildActivity(MainActivity.class).create().get();

【问题讨论】:

  • 您的测试运行配置中的工作目录是什么?
  • @EugenMartynov $MODULE_DIR$
  • Activity中有一些自定义视图吗?
  • @EugenMartynov:只是一个简单的 RelativeLayout,里面有一个 TextView。
  • @AnasAzeem 使用 RobolectricGradleTestRunner 而不是 RobolectricTestRunner 这应该可以解决您的问题

标签: java android unit-testing junit4 robolectric


【解决方案1】:

您的测试类注释是否正确?它是否使用清单文件的正确路径? 以下对我有用:

@RunWith(RobolectricTestRunner.class)
@Config(manifest = "src/main/AndroidManifest.xml", emulateSdk = Build.VERSION_CODES.LOLLIPOP)
public class MyActivityTest { .... }

删除 Config 注释会导致您提到的 ResourcesNotFound 异常。

【讨论】:

  • manifest = "src/main/AndroidManifest.xml" 部分可能有助于解决这个问题,但它会在以后给您带来越来越多的问题。而不是RobolectricTestRunner 使用RobolectricGradleTestRunner
  • emulateSdk 已弃用并从 Robolectric 3.0 中删除,请使用 sdk
【解决方案2】:

这对我使用 Robolectric 真的很有帮助! 打开 Gradle,双击 ProjectName > :app > Tasks > verification > testDebugUnitTest. 更多详情here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 2020-09-17
    • 1970-01-01
    • 2016-10-03
    相关资源
    最近更新 更多