简体   繁体   English

用robolectric运行单元测试get Resources $ NotFoundException

[英]Run unit test with robolectric get Resources$NotFoundException

The offical demo can't run correctly. 官方演示无法正确运行。 When I run the unit test demo, it show android.content.res.Resources$NotFoundException: String resource ID #0x7f050000 in the consle. 当我运行单元测试演示时,它在控制台中显示android.content.res.Resources $ NotFoundException:字符串资源ID#0x7f050000。

As the reason of network , I can't use m2e to create my project. 由于网络原因,我无法使用m2e创建我的项目。 But I download all the needed .jar with maven on another computer and copy the to this one, then add them into Eclipse IDE Java Build Path --> Library 但是我在另一台计算机上用maven下载了所有需要的.jar并将其复制到此计算机,然后将它们添加到Eclipse IDE Java Build Path-> Library

And here is my source code: 这是我的源代码:

public class MainActivity extends Activity {
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    View button = findViewById(R.id.login);
    button.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
        startActivity(new Intent(MainActivity.this, LoginActivity.class));
      }
    });
  }
}

and the test code are as follow: 和测试代码如下:

@RunWith(MyRunner.class)
public class MainActivityTest {
  @Test
  public void onCreateShouldInflateTheMenu() {
  Activity activity = Robolectric.setupActivity(MainActivity.class);

  final Menu menu = shadowOf(activity).getOptionsMenu();
  assertEquals("First menu item", menu.findItem(R.id.item1).getTitle());
  assertEquals("Second menu item", menu.findItem(R.id.item2).getTitle());
}

When I run the testcase with junit test, they following error info are shows in the "failure trace": 当我用junit test运行测试用例时,它们在“失败跟踪”中显示了以下错误信息:

android.content.res.Resources$NotFoundException: String resource ID #0x7f050000
at android.content.res.Resources.getText(Resources.java:312)
at android.content.res.Resources.getString(Resources.java:400)
at android.content.Context.getString(Context.java:409)
at org.robolectric.util.ActivityController.getActivityTitle(ActivityController.java:113)
at org.robolectric.util.ActivityController.attach(ActivityController.java:61)
at org.robolectric.util.ActivityController.of(ActivityController.java:32)
at org.robolectric.Robolectric.buildActivity(Robolectric.java:92)
at org.robolectric.Robolectric.buildActivity(Robolectric.java:88)
at org.robolectric.Robolectric.setupActivity(Robolectric.java:96)
at com.example.activity.MainActivityTest.clickingLogin_shouldStartLoginActivity(MainActivityTest.java:35)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:467)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:250)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:176)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:142)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

I also tried to override the RobolectricTestRunner and add the @Config tag, but it's the same result. 我还尝试覆盖RobolectricTestRunner并添加@Config标记,但是结果相同。 How can I fix the problem and run the demo successfully? 如何解决问题并成功运行演示?

您必须在build.gradle模块配置中将testOptions {unitTests {includeAndroidResources = true}}添加到a​​ndroid

It worked for me: 它为我工作:

android {
    ...
    testOptions {
        unitTests { includeAndroidResources = true }
    }
}

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

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