【问题标题】:How do I specify custom /res directory in Robolectric 2.2?如何在 Robolectric 2.2 中指定自定义 /res 目录?
【发布时间】:2014-01-31 08:39:08
【问题描述】:

我的单元测试和我的 android 应用位于不同的项目中。

使用 Robolectric 1,我可以像这样指定我的 /res 目录位置:

public class MyTestRunner extends RobolectricTestRunner {
    public MyTestRunner(Class<?> testClass) throws InitializationError {
        super(testClass, "../app/AndroidManifest.xml", "../app/res");
    }
}

如何在 Robolectric 2.2 中指定 /res 目录位置?

【问题讨论】:

    标签: android robolectric android-testing


    【解决方案1】:

    使用RobolectricTestRunner#getAppManifest(Config):

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

    【讨论】:

    • 从 Robolectric 2.x 开始,改用@Config 注解。
    • @Xian 当测试在src/test/java 中运行时,@Config 是否与gradle 构建一起使用?还是仍然需要RobolectricGradleTestRunner
    • 我不清楚为什么gradle 的世界如此不同;抱歉,不确定。
    【解决方案2】:

    前段时间我也有同样的问题,你所要做的就是在你的测试类上注释:

    @Config(manifest = "../app/AndroidManifest.xml")

    并且将从您的清单中为您提取依赖项。

    PS。还要确保您已使用 @RunWith(RobolectricTestRunner.class) 注释您的测试类,并使用适当的 JUnit 注释(@Before@Test@After 等)以及您的方法(以防万一您没有并且忘记了)。

    【讨论】:

    • 然后Robolectric 是否假定res 目录是AndroidManifest.xml 的兄弟(即:假定res../app 中)?
    • 是的,默认情况下,它会相对于 AndroidManifest.xml 查找 resassets。注意你也可以specify project-wide config settingslike src/test/resources/org.robolectric.Config.properties中的manifest:manifest=../app/AndroidManifest.xml
    猜你喜欢
    • 1970-01-01
    • 2013-12-30
    • 1970-01-01
    • 2011-02-24
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    • 2016-12-11
    • 1970-01-01
    相关资源
    最近更新 更多