Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在Robolectric 2.2中指定自定义/res目录?_Android_Robolectric_Android Testing - Fatal编程技术网

Android 如何在Robolectric 2.2中指定自定义/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",

我的单元测试和我的安卓应用在不同的项目中

使用Robolectric 1,我可以指定我的/res目录位置,如下所示:

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"));
}