Robolectric找不到资源或清单文件

卢加尔

我创建了一个新的TestProject并将以下行添加到我的testMethod中:

Robolectric.getShadowApplication().getString(R.string.mystring);

我的测试失败了

android.content.res.Resources$NotFoundException: unknown resource 2131558482

控制台显示以下警告:

WARNING: No manifest file found at .\..\..\MyProject\AndroidManifest.xml.Falling back to the Android OS resources only.
To remove this warning, annotate your test class with @Config(manifest=Config.NONE).
WARNING: no system properties value for ro.build.date.utc

获取字符串资源是否需要AndroidManifest.xml?我试图通过org.robolectric.Config.properties@Config添加清单,但是警告仍然出现,并且我无法获取字符串资源。我确保清单的相对路径正确。我也尝试更改JUnit运行配置,但这无济于事。

用户名

解决此处所述问题的方法:http : //blog.futurice.com/android_unit_testing_in_ides_and_ci_environments

失踪的清单

您现在应该已经注意到Robolectric抱怨无法找到您的Android清单。我们将通过编写自定义测试运行程序来解决此问题。将以下内容添加为app / src / test / java / com / example / app / test / RobolectricGradleTestRunner.java:

package com.example.app.test;

import org.junit.runners.model.InitializationError;
import org.robolectric.manifest.AndroidManifest;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.res.Fs;

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

  @Override
  protected AndroidManifest getAppManifest(Config config) {
    String myAppPath = RobolectricGradleTestRunner.class.getProtectionDomain()
                                                        .getCodeSource()
                                                        .getLocation()
                                                        .getPath();
    String manifestPath = myAppPath + "../../../src/main/AndroidManifest.xml";
    String resPath = myAppPath + "../../../src/main/res";
    String assetPath = myAppPath + "../../../src/main/assets";
    return createAppManifest(Fs.fileFromPath(manifestPath), Fs.fileFromPath(resPath), Fs.fileFromPath(assetPath));
  }
}

请记住在测试类中更改RunWith批注。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章