Robolectric not using test application

后端 未结 3 1801
抹茶落季
抹茶落季 2021-02-06 03:11

According to this link I can create a test application which Robolectric will automatically start using in tests. I cannot get this to work.

I am using Dagger for depen

3条回答
  •  臣服心动
    2021-02-06 03:58

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

提交回复
热议问题