[英]Dagger2 Injection Unit Tests is null
Hi i have used dagger for dependency injections of Network Module, ApplicationModule, DatabaseModule, Presenters and interactor in my app. 嗨,我已经使用匕首在我的应用程序中对网络模块,ApplicationModule,DatabaseModule,Presenter和交互器进行依赖项注入。 I want to use these same classes and Module during unit testing.
我想在单元测试期间使用这些相同的类和模块。
As unit testing reference, i have created AndroidTestAppComponent using following code: 作为单元测试参考,我使用以下代码创建了AndroidTestAppComponent:
@Singleton
@Component(modules = {
AndroidSupportInjectionModule.class,
AndroidTestAppModule.class,
NetworkModule.class
})
public interface AndroidTestAppComponent extends AndroidInjector<AndroidTestApplication> {
@Component.Builder
abstract class AndroidTestAppComponentBuilder extends Builder<AndroidTestApplication> {
}
}
Giving all module is out of scope for this question, consider AndroidTestAppModule.java below
: 考虑
AndroidTestAppModule.java below
所有模块超出此问题的范围,请考虑AndroidTestAppModule.java below
:
public class AndroidTestAppModule {
@Provides
@Singleton
Context provideContext(AndroidTestApplication application) {
return application.getApplicationContext();
}
@Singleton
@Provides
KeyguardManager provideKeyguardManager(Context context) {
return (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
}
@Singleton
@Provides
FingerprintManagerCompat providerFingerPrintManager(Context context) {
return FingerprintManagerCompat.from(context);
}
}
I am able to generate DaggerAndroidTestAppComponent
. 我能够生成
DaggerAndroidTestAppComponent
。 My Application class is as below: 我的应用程序类如下:
public class AndroidTestApplication extends DaggerApplication implements HasActivityInjector {
@Inject
DispatchingAndroidInjector<Activity> dispatchingActivityInjector;
AndroidInjector<AndroidTestApplication> androidInjector;
@Override
public void onCreate() {
super.onCreate();
androidInjector.inject(this);
}
@Override
protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
androidInjector = DaggerAndroidTestAppComponent.builder().create(this);
return androidInjector;
}
@Override
public DispatchingAndroidInjector<Activity> activityInjector() {
return dispatchingActivityInjector;
}
}
Some other AppPref.java
class 其他一些
AppPref.java
类
@Singleton
public class AppPref {
private SharedPreferences preferences;
@Inject
AppPref(Context context) {
preferences = context.getSharedPreferences("somefile", Activity.MODE_PRIVATE);
}
}
As read from documentation: AndroidInjection#inject(T t)
t here takes core android module, so when i call this in my Activity AndroidInjection.inject(activity_reference_usually__this__)
it works(Normal scenario, real build and no testing app) 正如从文档中读取的那样:
AndroidInjection#inject(T t)
t在这里需要核心的android模块,所以当我在Activity AndroidInjection.inject(activity_reference_usually__this__)
调用它时,它可以正常工作(正常情况下,实际构建并且没有测试应用程序)
Without changing much code how can i use these Classes in AndroidInstrumentationTest, because i will only change test implementation in Test**DaggerModules
inside test package. 在不更改太多代码的情况下,我将如何在AndroidInstrumentationTest中使用这些类,因为我只会在测试包内的
Test**DaggerModules
更改测试实现。
Sample code for instrumentation is given below: 仪器的示例代码如下:
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
AndroidTestApplication application;
@Inject
AppPref appPref;
@Before
public void setUp() throws IllegalAccessException, InstantiationException, ClassNotFoundException {
Context appContext = InstrumentationRegistry.getTargetContext();
application = (AndroidTestApplication) Instrumentation.newApplication(AndroidTestApplication.class, appContext);
DaggerAndroidTestAppComponent.builder().create(application).inject(application);
}
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("com.a.b", appContext.getPackageName());
}
@Test
public void testPreNotNUll() {
Assert.assertNotNull(appPref);
}
}
Ideally, apppref is alwyas null, becuase in setUp
method i have injected AndroidTestApplication class and not in ExampleInstrumentedTest
how can i edit my dagger2 code so that @Inject works fine and i get valid appPref object. 理想情况下,apppref始终为null,因为在
setUp
方法中我已经注入了ExampleInstrumentedTest
类,而不是在ExampleInstrumentedTest
中ExampleInstrumentedTest
了我如何编辑dagger2代码,以便@Inject可以正常工作并获得有效的appPref对象。 Thank you. 谢谢。
You are actually not injecting anything into your Test class. 实际上,您实际上没有在Test类中注入任何东西。
DaggerAndroidTestAppComponent.builder().create(application).inject(application);
You are injecting into AndroidTestApplication
instead of your Test. 您正在注入
AndroidTestApplication
而不是Test。
Try to add 尝试添加
void inject(ExampleInstrumentedTest test);
Into your Component interface. 进入您的组件界面。
@Override
protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
androidInjector = DaggerAndroidTestAppComponent.builder().create(this);
return androidInjector;
}
Here you are creating your Dagger Component, no need to do it again in the Test. 在这里,您将创建Dagger组件,而无需在测试中再次进行。 Make
androidInjector
to be AndroidTestAppComponent
instead of AndroidInjector
in your AndroidTestApplicaiton
, make a getter for that Component in your AndroidTestApplication
and then in your Test setUp
method use application.getComponent().inject(this);
让
androidInjector
被AndroidTestAppComponent
而不是AndroidInjector
在AndroidTestApplicaiton
,做一个getter方法在该组件AndroidTestApplication
,然后在测试setUp
方法使用application.getComponent().inject(this);
That way you are injecting dependencies into desired class which is your Test. 这样,您就可以将依赖项注入所需的类(即Test)中。
I had to modify @Component
interface to skip extending builder from AndroidInjector.Builder
and provide my own approach. 我不得不修改
@Component
接口,以跳过从AndroidInjector.Builder
扩展扩展AndroidInjector.Builder
并提供自己的方法。
@Singleton
@Component(modules = {
AndroidSupportInjectionModule.class,
AndroidTestAppModule.class,
NetworkModule.class
})
public interface AndroidTestAppComponent extends AndroidInjector<AndroidTestApplication> {
void inject(ExampleInstrumentedTest test);
@Component.Builder
abstract class AndroidTestAppComponentBuilder {
@BindsInstance
public abstract AndroidTestAppComponentBuilder application(AndroidTestApplication application);
public abstract AndroidTestAppComponent build();
}
}
Such that i had to manually pass application and build the component, then as suggested by tuby, i had to add new method void inject(ExampleInstrumentedTest test)
to @Component
interface. 这样,我必须手动传递应用程序并构建组件,然后根据tuby的建议,我必须向
@Component
接口添加新方法void inject(ExampleInstrumentedTest test)
。
My test class now looks like this and i am able to run test and get coverage[jacoco tool]: 我的测试类现在看起来像这样,我可以运行测试并获得覆盖率[jacoco工具]:
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Inject
AppPref appPref;
@Before
public void setUp() throws IllegalAccessException, InstantiationException, ClassNotFoundException {
Context appContext = InstrumentationRegistry.getTargetContext();
AndroidTestApplication application = (AndroidTestApplication) Instrumentation
.newApplication(AndroidTestApplication.class, appContext);
DaggerAndroidTestAppComponent.builder().application(application)
.build()
.inject(this);
}
@Test
public void test1AppPrefNotNUll() {
Assert.assertNotNull(appPref);
}
private final String KEY = "key";
private final String valid = "test_app";
private final String invalid = "non_app";
@Test
public void test2AppPrefWrite() {
appPref.writePreference(KEY, valid);
Assert.assertNotNull(appPref.readPreference(KEY));
}
@Test
public void test3AppPrefRead() {
Assert.assertEquals(valid, appPref.readPreference(KEY));
}
@Test
public void test4AppPrefInvalid() {
Assert.assertNotNull(invalid, appPref.readPreference(KEY));
}
@Test
public void test5AppPrefClear() {
appPref.clearPreferences();
Assert.assertEquals(0, appPref.size());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.