luk*_*jar 16 android android-manifest robolectric
我已经创建了一个新的TestProject并在我的testMethod中添加了以下行:
Robolectric.getShadowApplication().getString(R.string.mystring);
Run Code Online (Sandbox Code Playgroud)
我的测试失败了
android.content.res.Resources$NotFoundException: unknown resource 2131558482
Run Code Online (Sandbox Code Playgroud)
控制台显示以下警告:
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
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml是否需要获取字符串资源?我尝试通过org.robolectric.Config.properties和@Config添加Manifest,但警告仍然发生,我无法获取字符串资源.我确保清单的相对路径是正确的.我也试过更改JUnit运行配置,但这没有帮助.
小智 13
解决此处描述的问题:http: //blog.futurice.com/android_unit_testing_in_ides_and_ci_environments
失踪的清单
您现在应该注意到Robolectric抱怨无法找到您的Android Manifest.我们将通过编写自定义测试运行器来解决这个问题.将以下内容添加为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));
}
}
Run Code Online (Sandbox Code Playgroud)
请记住在测试类中更改RunWith批注.
我遇到了同样的错误,我们使用了几种口味和构建类型因此,有一些步骤可以使它工作:
1)Android studio测试运行配置
您还必须在Windows中将工作目录设置为$ MODULE_DIR $.http://robolectric.org/getting-started/应该这样说.
2)单元测试应该注释如下:
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21, manifest = "src/main/AndroidManifest.xml", packageName = "com.example.yourproject")
public class SomeFragmentTest {
Run Code Online (Sandbox Code Playgroud)
这里我们有清单文件和packageName的简单链接
从 4.0 版本开始,@Config 的使用并不受到高度赞赏,但是更新的入门指南解决了我的问题:http://robolectric.org/getting-started/
只需修改一处build.gradle,并加载Manifest文件,测试就可以运行。
android {
testOptions {
unitTests {
includeAndroidResources = true
}
}
}
Run Code Online (Sandbox Code Playgroud)
github上的相关问题(也已关闭):https ://github.com/robolectric/robolectric/issues/3328
| 归档时间: |
|
| 查看次数: |
12680 次 |
| 最近记录: |