
[英]Testing with Dagger 2 using separate component configurations in Android
[英]How to design an Android application using dagger with Testing in mind
我是匕首的新手,最近我开始在自己的一个项目中使用匕首,因为能够以不同的方式处理依赖注入的概念用于测试和生产,因此能够注入我可以用于测试的模拟对象很棒。
我修改了我的应用程序,以遵循dagger simple-android示例中的样式。
设置完毕后,我发现注入存在问题,并且我无法使用测试逻辑完全超载生产应用程序中的注入。
我正在寻找关于如何设置它的建议,以便我的测试可以实际注入与模拟或其他对象的差异,以便根据需要进行测试,而不是过于愚蠢。 目前, MainActivityTest被正确注入,但是当我们到达MainActivity时 ,它会转到PhoneApplication并使用它的对象图注入
我已经把我下面的内容包括在内了。 任何帮助将不胜感激!
这是我的PhoneApplication ,基于DemoApplication 。
public class PhoneApplication extends Application {
private ObjectGraph graph;
@Override
public void onCreate() {
super.onCreate();
graph = ObjectGraph.create(getModules().toArray());
}
protected List<Object> getModules() {
return Arrays.asList(new AndroidModule(this), new PhoneModule());
}
public void inject(Object object) {
graph.inject(object);
}
}
这是我的AndroidModule
@Module(library = true, injects = MainActivity.class)
public class AndroidModule {
private final Context context;
public AndroidModule(Context context) {
this.context = context;
}
/**
* Allow the application context to be injected but require that it be
* annotated with {@link ForApplication @Annotation} to explicitly
* differentiate it from an activity context.
*/
@Provides
@Singleton
@ForApplication
Context provideApplicationContext() {
return context;
}
@Provides
@Singleton
NotificationManager provideNotificationManager() {
return (NotificationManager) context
.getSystemService(Application.NOTIFICATION_SERVICE);
}
@Provides
@Singleton
LocalBroadcastManager provideLocalBroadcastManager() {
return LocalBroadcastManager.getInstance(context);
}
@Provides
@Singleton
ContentResolver provideContentResolver() {
return context.getContentResolver();
}
}
基于该示例,我还将我的活动设置为使用基本活动。
public abstract class ActionBarBaseActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((PhoneApplication) getApplication()).inject(this);
}
}
然后在我的MainActivity中,我有以下内容
public class MainActivity extends ActionBarBaseActivity {
...
@Inject
LocalBroadcastManager localBroadcastManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
try {
messageReceivedIntentFilter = new IntentFilter(
Constants.EVENT_MESSAGE_RECEIVED,
"vnd.android.cursor.dir/vnd."
+ DataProviderContract.AUTHORITY + "."
+ DataProviderContract.MESSAGES_TABLE_NAME);
localBroadcastManager.registerReceiver(messageReceiver,
messageReceivedIntentFilter);
} catch (MalformedMimeTypeException e) {
Log.e(LOG_TAG,
"An error occurred registering an Intent for EVENT_MESSAGE_RECEIVED",
e);
}
...
}
...
}
这很有效,注射很快就到了,我欣喜若狂。 直到我真的想做一些测试。 我想要执行的第一个测试是在我的MainActivity上 。
在上面的onCreate
方法中,我们使用AndroidModule中的LocalBroadcastManager而不是MainActivityTest中的LocalBroadcastManager,因为我们目前没有办法告诉PhoneApplication或活动他们应该使用不同的对象图。
public class MainActivityTest extends
ActivityInstrumentationTestCase2<MainActivity> {
@Inject
NotificationManager notificationManager;
@Inject
ContentResolver contentResolver;
@Inject
MockContentResolver mockContentResolver;
@Inject
LocalBroadcastManager localBroadcastManager;
private Context context;
public MainActivityTest() {
super(MainActivity.class);
}
@Module(injects = { MainActivityTest.class, MainActivity.class }, library = true, overrides = true)
static class MockModule {
Context context;
public MockModule(Context context) {
this.context = context;
}
@Provides
@Singleton
ContentResolver provideContentResolver() {
return provideMockContentResolver();
}
@Provides
@Singleton
MockContentResolver provideMockContentResolver() {
return new MockContentResolver();
}
@Provides
@Singleton
LocalBroadcastManager provideLocalBroadcastManager() {
return Mockito.mock(LocalBroadcastManager.class);
}
}
@Override
protected void setUp() throws Exception {
System.setProperty("dexmaker.dexcache", getInstrumentation()
.getTargetContext().getCacheDir().getPath());
context = getInstrumentation().getTargetContext();
ObjectGraph graph = ObjectGraph.create(new AndroidModule(context),
new MockModule(context));
graph.inject(this);
super.setUp();
};
@MediumTest
@UiThreadTest
public void testIncomingMessageReceiver_onReceive()
throws MalformedMimeTypeException {
ArgumentCaptor<BroadcastReceiver> receiverCaptor = ArgumentCaptor
.forClass(BroadcastReceiver.class);
Mockito.verify(localBroadcastManager, Mockito.atLeastOnce())
.registerReceiver(receiverCaptor.capture(),
Mockito.any(IntentFilter.class));
}
}
这是一个非常简单的测试,让我开始。 我知道在onCreate中,我们要注册一个BroadcastReceiver,所以让我们确保它已注册。 由于测试具有mockLocalBroadcastManager,但活动使用生产LocalBroadcastManager,因此验证失败。
我不确定。 刚刚通过网络搜索了解如何正确使用匕首进行测试。
然而,据我所知, MainActivity
从应用程序中获取其对象图。 所以,这是你必须插入MockModule
。
为此,您应该创建PhoneApplication
的子类并覆盖getModules()
方法以返回您的MockModule
。 之后,您应该使用ActivityUnitTestCase.setApplication()
模拟应用程序(您的测试应首先将ActivityUnitTestCase
子类化)。 这应该解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.