Android 如何在Robolectric 2.2中指定自定义/res目录?
我的单元测试和我的安卓应用在不同的项目中 使用Robolectric 1,我可以指定我的/res目录位置,如下所示:Android 如何在Robolectric 2.2中指定自定义/res目录?,android,robolectric,android-testing,Android,Robolectric,Android Testing,我的单元测试和我的安卓应用在不同的项目中 使用Robolectric 1,我可以指定我的/res目录位置,如下所示: public class MyTestRunner extends RobolectricTestRunner { public MyTestRunner(Class<?> testClass) throws InitializationError { super(testClass, "../app/AndroidManifest.xml",
public class MyTestRunner extends RobolectricTestRunner {
public MyTestRunner(Class<?> testClass) throws InitializationError {
super(testClass, "../app/AndroidManifest.xml", "../app/res");
}
}
公共类MyTestRunner扩展了RobolectrictTestRunner{
公共MyTestRunner(类testClass)引发初始化错误{
super(testClass,“../app/AndroidManifest.xml”,“./app/res”);
}
}
如何在Robolectric 2.2中指定/res目录位置?使用:
public MyTestRunner(类testClass)抛出初始化错误{
超级(测试类);
}
@凌驾
受保护的AndroidManifest getAppManifest(配置){
返回新的AndroidManifest(
Fs.fileFromPath(“../app/AndroidManifest.xml”),
Fs.fileFromPath(“../app/res”),
Fs.fileFromPath(“../app/assets”);
}
不久前我也有同样的问题,您所要做的就是用以下注释您的测试类:
@Config(manifest=“../app/AndroidManifest.xml”)
依赖项将从清单中为您提取
另外,还要确保您已经用
@RunWith(robollectrictestrunner.class)
注释了您的测试类,并用适当的JUnit注释(@Before
,@test
,@After
等)注释了您的方法(以防万一您没有这样做并且忘记了).是否假设Robolectric
目录是AndroidManifest.xml
的兄弟目录(即:假设res
位于。/app
)?是的,默认情况下,它查找res
和相对于AndroidManifest.xml
的资产。请注意,您也可以喜欢src/test/resources/org.roblectric.Config.properties
:manifest=../app/AndroidManifest.xml
中的清单,因为robolectric 2.x,所以使用注释。@Xian在src/test/java
中测试时,是否使用@Config
gradle
构建?还是仍然需要RobolectricGradleTestRunner
?我不清楚为什么gradle
世界如此不同;对不起,不确定。
public MyTestRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}
@Override
protected AndroidManifest getAppManifest(Config config) {
return new AndroidManifest(
Fs.fileFromPath("../app/AndroidManifest.xml"),
Fs.fileFromPath("../app/res"),
Fs.fileFromPath("../app/assets"));
}