我最近将 Dagger2 实现到一个 Android 应用程序中以便于依赖注入(inject),但在这样做之后我的一些测试已经停止工作。
现在我想了解如何调整我的测试以使用 Dagger2?我正在使用 Robolectric 来运行我的测试。
这是我如何使用 Dagger2,我最近才学会它,所以这可能是不好的做法,对测试没有帮助,所以请指出我可以做的任何改进。
我有一个 AppModule,如下所示:
@Module
public class MyAppModule {
//Application reference
Application mApplication;
//Set the application value
public MyAppModule(Application application) {
mApplication = application;
}
//Provide a singleton for injection
@Provides
@Singleton
Application providesApplication() {
return mApplication;
}
}
我称之为 NetworkModule,它提供如下注入(inject)对象:
@Module
public class NetworkModule {
private Context mContext;
//Constructor that takes in the required context and shared preferences objects
public NetworkModule(Context context){
mContext = context;
}
@Provides
@Singleton
SharedPreferences provideSharedPreferences(){
//...
}
@Provides @Singleton
OkHttpClient provideOKHttpClient(){
//...
}
@Provides @Singleton
Picasso providePicasso(){
//...
}
@Provides @Singleton
Gson provideGson(){
//...
}
}
然后组件是这样的:
Singleton
@Component(modules={MyAppModule.class, NetworkModule.class})
public interface NetworkComponent {
//Activities that the providers can be injected into
void inject(MainActivity activity);
//...
}
对于我的测试,我使用的是 Robolectric,我的应用程序类有一个测试变体,如下所示:
public class TestMyApplication extends TestApplication {
private static TestMyApplication sInstance;
private NetworkComponent mNetworkComponent;
@Override
public void onCreate() {
super.onCreate();
sInstance = this;
mNetworkComponent = DaggerTestMyApplication_TestNetworkComponent.builder()
.testMyAppModule(new TestMyAppModule(this))
.testNetworkModule(new TestNetworkModule(this)).build();
}
public static MyApplication getInstance() {
return sInstance;
}
@Override public NetworkComponent getNetComponent() {
return mNetworkComponent;
}
}
如您所见,我正在尝试确保使用我的 Dagger2 模块的模拟版本,这些模块也被模拟,模拟的 MyAppModule 返回 TestMyApplication,模拟的 NetworkModule 返回模拟对象,我还有一个模拟的 NetworkComponent,它扩展了真正的 NetworkComponent。
在测试设置中,我使用 Robolectric 创建 Activity ,如下所示:
//Build activity using Robolectric
ActivityController<MainActivity> controller = Robolectric.buildActivity(MainActivity.class);
activity = controller.get();
controller.create(); //Create out Activity
这将创建 Activity 并启动 onCreate,这就是问题所在,在 onCreate 中我有以下代码将 Activity 注入(inject)组件,以便它可以像这样使用 Dagger2:
@Inject Picasso picasso; //Injected at top of Activity
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
MyApplication.getInstance().getNetComponent().inject(this);
picasso.load(url).fetch();
这里的问题是,在运行测试时,我在 picasso 变量上得到了一个 NullPointerException,所以我猜我的 Dagger2 设置在某个地方缺少测试链接?
编辑:添加 TestNetworkModule
@Module
public class TestNetworkModule {
public TestNetworkModule(Context context){
}
@Provides
@Singleton
SharedPreferences provideSharedPreferences(){
return Mockito.mock(SharedPreferences.class);
}
@Provides @Singleton
Gson provideGson(){
return Mockito.mock(Gson.class);
}
@Provides @Singleton
OkHttpClient provideOKHttpClient(){
return Mockito.mock(OkHttpClient.class);
}
@Provides @Singleton
Picasso providePicasso(){
return Mockito.mock(Picasso.class);
}
}
最佳答案
您不需要将 setter 添加到您的 TestApplication 和模块中。您正在使用 Dagger 2,因此您也应该使用它在测试中注入(inject)依赖项:
首先在您的 MyApplication 中创建一个方法来检索 ApplicationComponent。此方法将在 TestMyApplication 类中被覆盖:
public class MyApplication extends Application {
private ApplicationComponent mApplicationComponent;
public ApplicationComponent getOrCreateApplicationComponent() {
if (mApplicationComponent == null) {
mApplicationComponent = DaggerApplicationComponent.builder()
.myAppModule(new MyAppModule(this))
.networkModule(new NetworkModule())
.build();
}
return mApplicationComponent;
}
}
然后创建一个 TestNetworkComponent:
@Singleton
@Component(modules = {MyAppModule.class, TestNetworkModule.class})
public interface TestApplicationComponent extends ApplicationComponent {
void inject(MainActivityTest mainActivityTest);
}
在 TestNetworkModule 中返回一个模拟
@Provides
@Singleton
Picasso providePicasso(){
return Mockito.mock(Picasso.class);
}
在您的 TestMyApplication 中,构建 TestNetworkComponent:
public class TestMyApplication extends MyApplication {
private TestApplicationComponent testApplicationComponent;
@Override
public TestApplicationComponent getOrCreateApplicationComponent() {
if (testApplicationComponent == null) {
testApplicationComponent = DaggerTestApplicationComponent
.builder()
.myAppModule(new MyAppModule(this))
.testNetworkModule(new TestNetworkModule())
.build();
}
return testApplicationComponent;
}
}
然后在您的 MainActivityTest 中使用应用程序标签运行并注入(inject)您的依赖项:
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21, application = TestMyApplication.class)
public class MainActivityTest {
@Inject
Picasso picasso;
@Before
public void setup() {
((TestMyApplication)RuntimeEnvironment.application).getOrCreateApplicationComponent().inject(this);
Mockito.when(picasso.load(Matchers.anyString())).thenReturn(Mockito.mock(RequestCreator.class));
}
@Test
public void test() {
Robolectric.buildActivity(MainActivity.class).create();
}
}
您的 Picasso 字段已注入(inject) Picasso 模型,现在您可以与其交互。
关于java - 如何在 Android 中使用 Dagger2 依赖注入(inject)和 Robolectric 进行测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38360587/