Robolectric:错误android.content.res.Resources $ NotFoundException

时间:2019-01-09 11:28:44

标签: java android unit-testing robolectric

  

Robolectric的buildActivity()调用出错。它越来越明显   文件正确,但出现错误“ android.content.res.Resources   NotFoundException:字符串资源ID 0x7f0b2de8“。

     

设置:Windows和robolectric-compile上的Android Studio 2.3.3   'org.robolectric:robolectric:2.4'和gradle插件,Android插件   2.3.2和GradleVersion 4.4

尝试了gradle.properties文件中的选项“ android.enableAapt2 = false”,但没有用。

@RunWith(RobolectricTestRunner.class)
@Config(manifest="src/main/AndroidManifest.xml", emulateSdk = 18)
public class SignalMonitorActivityTest {
    private SignalMonitorActivity objActivity;
    private ActivityController<SignalMonitorActivity> controller;
    @Before
    public void setUp() throws Exception {
         //Robolectric.setupActivity(SignalMonitorActivity.class);    
        controller = Robolectric.buildActivity(SignalMonitorActivity.class).create();
        controller.start();
        controller.visible();
        controller.resume();
    }

错误:

  

警告:ro.build.date.utc调试没有系统属性值:   从加载com.ABC.android.com_common_Configuration的资源   。\ src \ main \ res ...

     

android.content.res.Resources $ NotFoundException:字符串资源ID

     

0x7f0b2de8

     

在android.content.res.Resources.getText(Resources.java:239)处   org.robolectric.shadows.ShadowResources.getText(ShadowResources.java:363)     在android.content.res.Resources.getText(Resources.java)处   android.content.res.Resources.getString(Resources.java:325)在   org.robolectric.shadows.ShadowContext.getString(ShadowContext.java:41)     在   org.robolectric.shadows.ShadowContextWrapper.getString(ShadowContextWrapper.java:96)     在android.content.Context.getString(Context.java)   org.robolectric.util.ActivityController.getActivityTitle(ActivityController.java:101)     在   org.robolectric.util.ActivityController.attach(ActivityController.java:44)     在   org.robolectric.util.ActivityController $ 1.run(ActivityController.java:114)     在   org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:268)     在   org.robolectric.util.ActivityController.create(ActivityController.java:111)     在   org.robolectric.util.ActivityController.create(ActivityController.java:122)     在   com.ABC.android.combine.diagnostics.SignalMonitorActivityTest.setUp(SignalMonitorActivityTest.java:29)

1 个答案:

答案 0 :(得分:0)

尝试在build.gradle

中添加以下内容
android {
  testOptions {
    unitTests {
      includeAndroidResources = true
    }
  }
}

dependencies {
  testImplementation 'org.robolectric:robolectric:2.4'
}

尝试在gradle.properties

中添加它
android.enableUnitTestBinaryResources=true