mercredi 8 juillet 2015

Roboguice injection doesn't work when running tests via Android Studio

I have updated the tests to Robolectric v3 rc-3 and using Roboguice v3 for DI. The injections work fine when running the tests via Gradle testDebug task but when running via Android Studio the injections throws NPE.

I have defined a custom module for injections in the test class, and override the default module using the following:

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);

    ShadowLog.stream = System.out;
    Application application = RuntimeEnvironment.application;
    mContext = application;

    ShadowApplication shadowApplication = Shadows.shadowOf(RuntimeEnvironment.application);
    // To avoid null pointer exception when buildActivity is called.
    shadowApplication.declareActionUnbindable("com.google.android.gms.analytics.service.START");

    initMocks();

    Module module = getModule();

    if (module == null) {
        module = defaultModule();
    }

    RoboGuice.overrideApplicationInjector(application, module);
}

Any idea, what is the difference between running via gradle task vs Android Studio?

Aucun commentaire:

Enregistrer un commentaire