简体   繁体   English

Robolectric不使用测试应用程序

[英]Robolectric not using test application

According to this link I can create a test application which Robolectric will automatically start using in tests. 根据这个链接,我可以创建一个测试应用程序,Robolectric将自动开始在测试中使用。 I cannot get this to work. 我不能让这个工作。

I am using Dagger for dependency injection and have created injection wrapper classes for Activity and Application . 我正在使用Dagger进行依赖注入,并为ActivityApplication创建了注入包装类。 Then each of my activities extends the wrapper activity class instead of plain old Activity . 然后我的每个活动都扩展了包装器活动类而不是普通的旧Activity

The problem I am having is that, in tests, the dependencies provided by the Application modules can't be resolved and so the tests fail. 我遇到的问题是,在测试中, Application模块提供的依赖关系无法解析,因此测试失败。 This is because most of our tests are just building an activity (using Robolectric.buildActivity() ) and aren't running from an Application . 这是因为我们的大多数测试只是构建一个活动(使用Robolectric.buildActivity() )而不是从Application运行。

I was hoping to somehow modify the Robolectric testrunner to run our tests under the Application . 我希望以某种方式修改Robolectric testrunner以在Application下运行我们的测试。 Either that or use a test application as outlined in that link above. 或者使用上面链接中概述的测试应用程序。

I've created a test application and am still getting the same test errors because the tests aren't running under this test application. 我已经创建了一个测试应用程序,但仍然遇到相同的测试错误,因为测试未在此测试应用程序下运行。 I've tried moving the test application to different packages etc, but nothing changes. 我已经尝试将测试应用程序移动到不同的包等,但没有任何改变。

I'm looking for some advice on how to go about doing what I want. 我正在寻找一些关于如何去做我想要的建议。 Would be particularly interested in those with experience with Dagger and how they go about testing. 对那些有Dagger经验以及他们如何进行测试的人特别感兴趣。

It's really simple in Robolectric 3.0, you add it directly to the @Config annotation. 在Robolectric 3.0中它非常简单,你可以直接将其添加到@Config注释中。

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21,application = TestApplication.class)
public class ActivityTest {

Apologies, forgot about this. 道歉,忘记了这一点。 To resolve this I created a TestApplication residing along side the tests. 为了解决这个问题,我在测试中创建了一个TestApplication Then I modified our TestRunner (which extends the RobolectricTestRunner ) to: 然后我修改了我们的TestRunner (它将RobolectricTestRunner扩展到):

public class TestRunner extends RobolectricTestRunner {

    public TestRunner(final Class<?> testClass) throws InitializationError {
        super(testClass);
    }

    ...

    @Override
    protected Class<? extends TestLifecycle> getTestLifecycleClass() {
        return MyTestLifecycle.class;
    }

    public static class MyTestLifecycle extends DefaultTestLifecycle {
        @Override
        public Application createApplication(final Method method, final AndroidManifest appManifest) {
            // run tests under our TestApplication
            return new TestApplication();
        }
    }

    ...

}

You can configure it in the file org.robolectric.Config.properties 您可以在org.robolectric.Config.properties文件中对其进行配置

application = <fully qualified name of the Application>

See http://robolectric.org/configuring/ http://robolectric.org/configuring/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM