With Roboblectric, you can extend RobolectricTestRunner
and override getTestLifecycleClass()
to easily hook into the lifecycle of the test suite. This easily allows you to force RoboGuice to use whatever test modules you want, without the need to subclass individual test case classes.
The prepareTest
method is especially useful in the next example:
An Example with Robolectric:
public class RobolectricTestRunnerWithInjection extends RobolectricTestRunner {
public RobolectricTestRunnerWithInjection(Class<?> testClass) throws InitializationError {
super(testClass);
}
@Override
protected Class<? extends TestLifecycle> getTestLifecycleClass() {
return TestLifeCycleWithInjection.class;
}
public static class TestLifeCycleWithInjection extends DefaultTestLifecycle {
private MyApplication application;
@Override
public Application createApplication(Method method, AndroidManifest appManifest, Config config) {
application = (MyApplication) super.createApplication(method, appManifest, config);
return application;
}
@Override
public void prepareTest(Object test) {
Module testApplicationModule = new TestApplicationModule();
if (test instanceof AbstractModule) {
testApplicationModule = Modules.override(testApplicationModule).with((AbstractModule) test);
}
RoboGuice.overrideApplicationInjector(application, RoboGuice.newDefaultRoboModule(application), testApplicationModule);
RoboGuice.getInjector(application).injectMembers(test);
MockitoAnnotations.initMocks(test);
}
}
}
Is there a similar way to do this using Android's default test suite (http://ift.tt/1FjTxBa)?
I would prefer not to subclass test case classes (i.e. AndroidTestCase, ApplicationTestCase, etc).
Aucun commentaire:
Enregistrer un commentaire