简体   繁体   English

调用Robolectric.buildActivity()时出现Resources $ NotFoundException

[英]Resources$NotFoundException when calling Robolectric.buildActivity()

I am using Robolectric 3.0 RC3 for unit testing in Android Studio. 我正在使用Robolectric 3.0 RC3在Android Studio中进行单元测试。 And I am getting a ResourceNotFoundException every time I run the test. 每次运行测试时,我都会收到ResourceNotFoundException Please help me resolve the issue. 请帮助我解决问题。

build.gradle 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'
}

Test Class 测试班

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

So, whenever Robolectric.buildActivity() I get the Resources$NotFoundException . 因此,无论何时Robolectric.buildActivity()我都会得到Resources$NotFoundException Resource #0x7f040016 points to R.layout.activity_main (xml). 资源#0x7f040016指向R.layout.activity_main (xml)。 Following id the stack trace- 在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:15 is setContentView(R.layout.activity_main); MainActivity.java:15setContentView(R.layout.activity_main);

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

Is your test class annotated correctly? 您的测试课是否正确注解? And is it using the correct path to a manifest file? 是否使用了清单文件的正确路径? The following works for me: 以下对我有用:

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

Removing the Config annotation causes the ResourcesNotFound exception you mentioned. 删除Config批注会导致您提到的ResourcesNotFound异常。

This is realy help for me with Robolectric! 这对Robolectric确实是我的帮助! Open Gradle, Double click on ProjectName > :app > Tasks > verification > testDebugUnitTest. 打开Gradle,双击ProjectName>:app> Tasks>验证> testDebugUnitTest。 就像图片一样 More details here 在这里更多细节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM