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
You can configure it in the file org.robolectric.Config.properties
application = <fully qualified name of the Application>
See http://robolectric.org/configuring/
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();
}
}
...
}