i yansıma kullanmamayı tercih hangi android üzerinde yavaş. Çoğumuz bağımlılık enjeksiyonu için ayarlanmış hançer2 var. Test için kurulmuş bir test bileşenim var.
@Module
public class AppModule {
@Provides
public ApplicationMode provideApplicationMode(){
return ApplicationMode.NORMAL;
}
}
benim gibi bir test koşucu oluşturun:
public enum ApplicationMode {
NORMAL,TESTING;
}
ve normal AppModule:
bir enum oluşturmak: Burada uygulama modunu alabilirsiniz kısa yolu (test ya da normal) 'dir :
public class PomeloTestRunner extends AndroidJUnitRunner {
@Override
public Application newApplication(ClassLoader cl, String className, Context context) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
return super.newApplication(cl, MyTestApplication.class.getName(), context);
}
}
böyle gradle içinde beyan etmek unutma:
Şimdi
defaultConfig {
testInstrumentationRunner "com.mobile.pomelo.base.PomeloTestRunner"
}
böyle tam olarak görünüyor geçersiz kılma yöntemi ile AppModule bir alt sınıfını oluşturabilir ve sınıf tanımı üzerinde bir modül olarak işaretlemek etmeyin: MyTestApplication sınıfında şimdi
public class TestAppModule extends AppModule{
public TestAppModule(Application application) {
super(application);
}
@Override
public ApplicationMode provideApplicationMode(){
return ApplicationMode.TESTING; //notice we are testing here
}
}
sen bildirilen özel test atlet ilan aşağıdakiler var:
Şimdi
public class PomeloTestApplication extends PomeloApplication {
@Singleton
@Component(modules = {AppModule.class})
public interface TestAppComponent extends AppComponent {
}
@Override
protected AppComponent initDagger(Application application) {
return DaggerPomeloTestApplication_TestAppComponent.builder()
.appModule(new TestAppModule(application)) //notice we pass in our Test appModule here that we subclassed which has a ApplicationMode set to testing
.build();
}
}
sadece kullanmak yerde böyle üretim kodunda enjekte etmek:
@Inject
ApplicationMode appMode;
çalışan espresso testleriniz enum testini yaparken, üretim kodunda normal enum olacak.
ps gerekli değildir ama sen benim üretim hançer onun böyle grafiği oluşturur ve uygulama alt sınıfta beyan nasıl gerekiyorsa: Ben gibi iki dosya oluşturacağız
protected AppComponent initDagger(Application application) {
return DaggerAppComponent.builder()
.appModule(new AppModule(application))
.build();
}
Test kodunuzun VM'de olup olmadığını görmek için bir hacky çözümü 'Class.forName()' dır: http://wtanaka.com/node/8041 – CommonsWare
Bu sorunu çözdünüz mü? – Marcus
Evet. – Comtaler