Robolectric不使用测试应用程序 [英] Robolectric not using test application
问题描述
根据此链接,我可以创建一个测试应用程序Robolectric将自动开始在测试中使用。我无法使用它。
According to this link I can create a test application which Robolectric will automatically start using in tests. I cannot get this to work.
我正在使用Dagger进行依赖项注入,并为 Activity
创建了注入包装类。和应用程序
。然后,我的每个活动都扩展了包装活动类,而不是普通的 Activity
。
I am using Dagger for dependency injection and have created injection wrapper classes for Activity
and Application
. Then each of my activities extends the wrapper activity class instead of plain old Activity
.
我遇到的问题是在测试中, Application
模块提供的依赖项无法解析,因此测试失败。这是因为我们的大多数测试只是在构建活动(使用 Robolectric.buildActivity()
),而不是从 Application
。
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. This is because most of our tests are just building an activity (using Robolectric.buildActivity()
) and aren't running from an Application
.
我希望以某种方式修改Robolectric测试运行程序以在 Application
下运行测试。要么使用上面的链接中概述的测试应用程序。
I was hoping to somehow modify the Robolectric testrunner to run our tests under the 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.
我正在寻找有关如何做自己想做的建议。对那些有Dagger经验的人以及他们如何进行测试会特别感兴趣。
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.
推荐答案
道歉,忘了这个。为了解决这个问题,我在测试旁边创建了一个 TestApplication
。然后,我将 TestRunner
(这扩展了 RobolectricTestRunner
)修改为:
Apologies, forgot about this. To resolve this I created a TestApplication
residing along side the tests. Then I modified our TestRunner
(which extends the RobolectricTestRunner
) to:
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();
}
}
...
}
这篇关于Robolectric不使用测试应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!