Robolectric not using test application

旧时模样 提交于 2019-12-03 03:51:31
Ernir Erlingsson

It's really simple in Robolectric 3.0, you add it directly to the @Config annotation.

@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. 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();
        }
    }

    ...

}

You can configure it in the file org.robolectric.Config.properties

application = <fully qualified name of the Application>

See http://robolectric.org/configuring/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!