android - Dagger2 disable AndroidInjection.inject(this); for instrumentation test -


with latest dagger2 can inject dependencies through following code:

public class baseactivity extends lifecycleactivity {      @override     protected void oncreate(bundle savedinstancestate) {         androidinjection.inject(this);         super.oncreate(savedinstancestate);     } } 

other activities require di inherit baseclass. instrumentation tests, don't want use dagger, can mock objects.

my setup instrumentation test (i'm testing loginactivity, extends baseactivity) not special , follows:

testrunner:

public class livefeedtestrunner extends androidjunitrunner{     @override     public application newapplication(classloader cl, string classname, context context)             throws instantiationexception, illegalaccessexception, classnotfoundexception {         return super.newapplication(cl, testapp.class.getname(), context);     } } 

testapp:

public class testapp extends application {     @override     public void oncreate() {         super.oncreate();     } } 

the problem running instrumentation test results in error caused by:

caused by: java.lang.runtimeexception: com.example.kimgysen.livefeed_v002.testapp not implement dagger.android.hasactivityinjector @ dagger.android.androidinjection.inject(androidinjection.java:48) @ com.example.kimgysen.livefeed_v002.ui.baseactivity.oncreate(baseactivity.java:12) @ com.example.kimgysen.livefeed_v002.ui.login.loginactivity.oncreate(loginactivity.java:32) @ android.app.activity.performcreate(activity.java:6237) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1107) @ android.support.test.runner.monitoringinstrumentation.callactivityoncreate(monitoringinstrumentation.java:624) @ android.app.activitythread.performlaunchactivity(activitythread.java:2369) ... 9 more

which points following line:

public class baseactivity extends lifecycleactivity {      @override     protected void oncreate(bundle savedinstancestate) {         androidinjection.inject(this); // <-- line          super.oncreate(savedinstancestate);     } } 

any idea how can bypass this? in app itself, implement dagger.android.hasactivityinjector, don't need instrumentation test.

i solved problem creating fake activityinjector suggested in following post:
https://github.com/google/dagger/blob/master/javatests/dagger/android/androidinjectiontest.java#l60

(equivalent fakeactivityinjector)

public class testapp extends application implements hasactivityinjector {     @override     public void oncreate() {         super.oncreate();     }      @override     public androidinjector<activity> activityinjector() {         return fakeactivityinjector("injected app");     }      public static class injectableactivity extends activity {         string tag;     }      private static androidinjector<activity> fakeactivityinjector(string tag) {         return instance -> {             if (instance instanceof injectableactivity) {                 ((injectableactivity) instance).tag = tag;             }         };     } } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -