【问题标题】:Robolectric not using test applicationRobolectric 不使用测试应用程序
【发布时间】:2014-02-12 10:33:31
【问题描述】:

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

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

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

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

我创建了一个测试应用程序,但仍然收到相同的测试错误,因为测试没有在此测试应用程序下运行。我已经尝试将测试应用程序移动到不同的包等,但没有任何变化。

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

【问题讨论】:

  • 分享代码。我无法理解“在应用程序下运行我们的测试”。我们将 robolectric 与 dagger 一起使用,并且我们的测试应用程序工作正常

标签: android robolectric dagger


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    抱歉,忘记了。为了解决这个问题,我在测试旁边创建了一个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/

      【讨论】:

      • 只是为了更新,在 3.0 中不再是这种情况。该文件现在称为 robolectric.properties。
      猜你喜欢
      • 2018-07-07
      • 1970-01-01
      • 1970-01-01
      • 2013-08-19
      • 2012-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-06
      相关资源
      最近更新 更多