mardi 31 mars 2015

Roboguice CreationException in test

I'm new to Roboguice topic... I'm trying to correctly instantiate the injector inside the Application.onCreate() method. I tried in two ways:



  1. RoboGuice.overrideApplicationInjector(this, RoboGuice.newDefaultRoboModule(this), new SettingsModule());

  2. RoboGuice.getOrCreateBaseApplicationInjector(this, RoboGuice.DEFAULT_STAGE, Modules.override(RoboGuice.newDefaultRoboModule(this)).with(new SettingsModule()));


When running one of the AndroidTestCase I have I ended up with two different results depending on if I used 1 or 2:



  1. worked correctly

  2. thrown a com.google.inject.CreationException for this specific reason:



Could not find a suitable constructor in my.package.SharedPreferencesStorage. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.



The problem is that SharedPreferencesStorage has only one constructor annotated with @Inject, so the exception is quite misleading.


At this point I could go on using the overrideApplicationInjector() in my Application but according to the documentation it should be used only in testing. Is it correct? Is there some implication on using it outside a testing class? Why getOnCreateBaseApplicationInjector() doesn't work instead? Any hint is appreciated!


Aucun commentaire:

Enregistrer un commentaire