未找到Robolectric 3资源错误

时间:2015-08-20 05:55:51

标签: java android unit-testing robolectric

我正在尝试将Robolectric 3纳入我的项目中,但我似乎遇到的问题比我预期的要多。目前,我试图通过

在我的测试类的setUp方法中实例化一个简单的活动
MyActivity activity = Robolectric.setupActivity(MyActivity.class);

我已将以下内容放在测试文件的顶部:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class)

在MyActivity中,我只需通过以下方式访问字符串数组资源:

getResources().getStringArray(resId)

运行测试类后(没有任何测试方法,只有实例化活动的setUp方法),我收到错误:

android.content.res.Resources$NotFoundException: String array resource ID #0x7f0c0000
at android.content.res.Resources.getStringArray(Resources.java:502)
at com.alivemobile.tpff.slidingtabs.views.SlidingTabsActivity$3.onItemSelected(SlidingTabsActivity.java:102)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:897)
at android.widget.AdapterView.access$200(AdapterView.java:48)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:865)
at org.robolectric.util.Scheduler.runOrQueueRunnable(Scheduler.java:230)
at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:85)
at org.robolectric.util.Scheduler.post(Scheduler.java:72)
at org.robolectric.shadows.ShadowView.post(ShadowView.java:401)
at android.view.View.post(View.java)
at android.widget.AdapterView.selectionChanged(AdapterView.java:882)
at android.widget.AdapterView.checkSelectionChanged(AdapterView.java:1047)
at android.widget.Spinner.layout(Spinner.java:586)
at android.widget.Spinner.onLayout(Spinner.java:527)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.support.v7.widget.Toolbar.layoutChildLeft(Toolbar.java:1565)
at android.support.v7.widget.Toolbar.onLayout(Toolbar.java:1504)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2072)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1829)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at org.robolectric.shadows.ShadowMessageQueue.dispatchMessage(ShadowMessageQueue.java:130)
at org.robolectric.shadows.ShadowMessageQueue.access$100(ShadowMessageQueue.java:30)
at org.robolectric.shadows.ShadowMessageQueue$1.run(ShadowMessageQueue.java:95)
at org.robolectric.util.Scheduler$ScheduledRunnable.run(Scheduler.java:269)
at org.robolectric.util.Scheduler.runOneTask(Scheduler.java:179)
at org.robolectric.util.Scheduler.advanceTo(Scheduler.java:160)
at org.robolectric.util.Scheduler.advanceBy(Scheduler.java:143)
at org.robolectric.util.Scheduler.unPause(Scheduler.java:54)
at org.robolectric.shadows.ShadowLooper.unPause(ShadowLooper.java:266)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:306)
at org.robolectric.shadows.CoreShadowsAdapter$2.runPaused(CoreShadowsAdapter.java:45)
at org.robolectric.util.ActivityController.visible(ActivityController.java:173)
at org.robolectric.util.ActivityController.setup(ActivityController.java:210)
at org.robolectric.Robolectric.setupActivity(Robolectric.java:46)
at com.alivemobile.tpff.events.views.list.EventsListTests.setUp(EventsListTests.java:20)
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:251)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
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:152)
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)

我已经google了一下,看到一些人实现了一个自定义测试运行器,我尝试了一些,但他们看起来都过于复杂,没有太多解释发生了什么。 我错过了什么?

提前致谢。

更新1 玩了一下后,我发现如果我在setUp()方法中实例化活动:

Robolectric.buildActivity(SlidingTabsActivity.class).create().start().resume().get();

而不是:

Robolectric.setupActivity(SlidingTabsActivity.class);

测试未发现资源未找到错误。

但是如果我用以下方式实施活动:

Robolectric.buildActivity(SlidingTabsActivity.class).create().start().resume().visible().get();

然后我收到资源未找到错误。

3 个答案:

答案 0 :(得分:1)

我们在使用robolectric的测试中遇到了类似的问题。这是因为我们有不同的构建风格,它们与我们的包结构有不同的applicationId。 RobolectricGradleTestRunner类默认从src / main文件夹中选择资源,资产和清单。因此,您不需要实现自定义测试运行器。 检查gradle构建中的applicationId是否与包结构不同。您的applicationId可能是com.something.abc,您的基本包可能是com.something.xyz 如果您为每个构建版本使用不同的构建版本和applicationId,那么我建议您在测试套件中添加以下内容。

@Config(constants = BuildConfig.class, packageName = "com.something.xyz")

答案 1 :(得分:1)

您可以像这样创建CustomRobolectricTestRunner

import org.junit.runners.model.InitializationError;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

public class CustomRobolectricTestRunner extends RobolectricTestRunner {
public CustomRobolectricTestRunner(Class<?> testClass) throws InitializationError {
    super(testClass);
}

@Override
protected Config buildGlobalConfig() {
    return Config.Builder.defaults()
            .setPackageName("com.x")
            .setManifest("build/intermediates/manifest/androidTest/debug/AndroidManifest.xml")
            .setResourceDir("../../../../res/merged/androidTest/debug") // relative to manifest
            .build();
}
}

清单文件和资源目录的路径应该是正确的。因此,您可以在项目的build/intermediates目录中检查它的正确路径。

注意:资源目录路径需要与清单路径相关。

在您的测试课程中,您需要像这样使用CustomRobolectricTestRunner

@org.junit.runner.RunWith(CustomRobolectricTestRunner.class)
public class MyTestClass {
}

答案 2 :(得分:0)

我们有所有资源的默认包,这个自定义TestRunner可以帮助我们。

public class RobolectricGradleTestRunner extends RobolectricTestRunner {

    public RobolectricGradleTestRunner(final Class<?> testClass) throws InitializationError {
        super(testClass);
    }

    @Override
    protected AndroidManifest getAppManifest(final Config config) {
        final String myAppPath = RobolectricGradleTestRunner.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        final String manifestPath = myAppPath + "../../../../manifests/full/" + BuildConfig.FLAVOR + "/" + BuildConfig.BUILD_TYPE + "/AndroidManifest.xml";
        final String resPath = myAppPath + "../../../../res/merged/" + BuildConfig.FLAVOR + "/" + BuildConfig.BUILD_TYPE;
        final String assetPath = myAppPath + "../../../../assets/" + BuildConfig.FLAVOR + "/" + BuildConfig.BUILD_TYPE;

        return new AndroidManifest(Fs.fileFromPath(manifestPath),
                Fs.fileFromPath(resPath),
                Fs.fileFromPath(assetPath),
                BuildConfig.APPLICATION_ID) {

            @Override
            public String getRClassName() throws Exception {
                return "[your_package_name].R";
            }

        };
    }
}