2013-05-06 19 views
5

RoboGuice 2.0 kullanarak bir Android Hizmeti için bir JUnit testi yazmaya çalışıyorum. Mockito alay nesnelerine enjekte bağımlılıkları bağlayan bir test modülüm var. Ancak, testi çalıştırdığımda, uygulama modülümden gerçek uygulamalar yerine enjekte edilir.RoboGuice birim test modülünü test modülü yerine enjekte ediyor

MainApplication.java:

public class MainApplication extends Application { 
    @Override 
    public void onCreate() { 
     super.onCreate(); 
     RoboGuice.setBaseApplicationInjector(this, RoboGuice.DEFAULT_STAGE, 
      RoboGuice.newDefaultRoboModule(this), new MainModule()); 
     startService(new Intent(this, NotificationService.class)); 
    } 
} 

MainModule.java:

public class MainModule extends AbstractModule { 
    @Override 
    protected void configure() { 
     bind(IFooManager.class).to(FooManagerImpl.class).in(Scopes.SINGLETON); 
    } 
} 

NotificationService.java:

public class NotificationService extends RoboService { 
    @Inject 
    private NotificationManager notificationManager; 
    @Inject 
    private SharedPreferences prefs; 
    @Inject 
    private IFooManager fooManager; 
    private IFooListener listener = new FooListener(); 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     fooManager.addListener(listener); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     fooManager.removeListener(listener); 
    } 

    private class FooListener implements IFooListener { 
     // Do stuff that fires Notifications 
    } 
} 

NotificationServiceTest.java:

İşte alakalı kod bazı olduğunu

MockFooManager ve MockSharedPreferences, RoboGuice can't inject mocks of interfaces nedeniyle gerekli olan IFooManager ve SharedPreferences öğelerinin boş soyut uygulamalarıdır. Ben alaylı sınıflar için bytecode nesil desteklemek için Dexmaker ile Mockito kullanıyorum. Ayrıca, Robolectric kullanmıyorum, bu testleri bir cihazda veya emülatörde çalıştırıyorum.

Bu testi çalıştırdığımda, Wanted but not invoked: fooManager.addListener(isA(com.example.IFooListener)) hatasını alıyorum. Hata ayıklayıcısını kullanarak bu adımı attıktan sonra, RoboGuice'in TestModule yerine MainModule'den bağımlılıkları enjekte ettiğini buldum, bu yüzden test MockFooManager yerine FooManagerImpl kullanıyor. RoboGuice'in test kodunda MainModule hakkında nasıl olduğunu bile bilmiyorum. İşte

Bunu düzeltmek için çalıştı başka şeyler vardır, ama hiçbiri herhangi bir etkisi vardı:

  • yerine RoboGuice.setBaseApplicationInjector çağrılırken Kullanım Modules.overrideMainApplication.onCreate
  • yılında RoboGuice.setBaseApplicationInjector aramak yerine sadece geçme roboguice.xml konusu uygulama modüllerini belirtin modül listesi doğrudan.

RoboGuice'i TestModule'ı kullanmaya ve birim testimde MainModule'ı dikkate almaya nasıl başlarım?

cevap

0

NotificationServiceTest'inizde enjeksiyon yapmak için bir çağrı eksik gibi görünüyor. Bu şöyle yapılır:

RoboGuice.getInjector(app).injectMembers(this); 

Sen testler çalıştırılır önce taban enjektörü ayarlamak ve sonra bir noktada bu eklemek gerekir.

0

Kullanım

RoboGuice.overrideApplicationInjector(app,RoboGuice.newDefaultRoboModule(app), new TestModule()) 

yerine

RoboGuice.setBaseApplicationInjector(app, RoboGuice.DEFAULT_STAGE, new TestModule());