2014-02-12 166 views
8

根据this link我可以创建一个测试应用程序,Robolectric将在测试中自动开始使用。我无法得到这个工作。Robolectric未使用测试应用程序

我使用Dagger进行依赖注入,并为ActivityApplication创建了注入包装类。然后,我的每个活动都会扩展包装活动类,而不是普通的旧Activity

我遇到的问题是,在测试中,由Application模块提供的依赖关系无法解析,因此测试失败。这是因为我们的大多数测试只是建立一个活动(使用Robolectric.buildActivity()),而不是从Application运行。

我曾希望以某种方式修改Robolectric testrunner以在Application下运行我们的测试。无论是或者使用上面链接中概述的测试应用程序。

我创建了一个测试应用程序,并且仍然得到相同的测试错误,因为这个测试应用程序没有运行测试。我试着将测试应用程序移动到不同的软件包等,但没有任何变化。

我正在寻找一些关于如何去做我想做的事的建议。对那些有Dagger经验的人以及他们如何进行测试会特别感兴趣。

+0

分享该代码。我无法理解“在应用程序下运行我们的测试”。我们使用robolectric与匕首,我们有测试应用程序正常工作 –

回答

4

道歉,忘了这件事。为了解决这个问题,我创建了一个位于测试旁边的TestApplication。然后我修改了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(); 
     } 
    } 

    ... 

} 
3

您可以在文件org.robolectric.Config.properties配置它

application = <fully qualified name of the Application> 

http://robolectric.org/configuring/

+1

只是为了更新,3.0中不再是这种情况。该文件现在称为robolectric.properties。 –

8

这真的很简单在Robolectric 3.0中,您直接将其添加到@Config注释中。

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