单元测试 Activity.startService() 调用?
尝试编写我的第一个 Android-by-TDD 应用程序(我已经编写了一些没有 TDD 的小型 Android 应用程序,因此熟悉环境),但我似乎不知道如何编写我的第一个测试。
场景:
我有一个活动 TasksActivity 和一个服务 TasksService。我需要测试 TasksActivity 是否在其 onStart() 方法中启动 TasksService。
我编写的测试是这样的:
public class ServiceControlTest extends ActivityUnitTestCase<TasksActivity>{
public ServiceControlTest() {
super(TasksActivity.class);
}
public void testStartServiceOnInit () {
final AtomicBoolean serviceStarted = new AtomicBoolean(false);
setActivityContext(new MockContext() {
@Override
public ComponentName startService(Intent service) {
Log.v("mockcontext", "Start service: " + service.toUri(0));
if (service.getComponent().getClassName().equals (TasksService.class.getName()))
serviceStarted.set(true);
return service.getComponent();
}
});
startActivity(new Intent(), null, null);
assertTrue ("Service should have been started", serviceStarted.get());
}
}
在 TasksActivity 的 onCreate() 方法中,我有:
startService(new Intent(this, TasksService.class));
我也尝试过,
getBaseContext().startService(new Intent(this, TasksService.class));
但在这两种情况下,我的 MockContext 的 startService 方法都不会被调用。有没有办法可以设置此方法的拦截?我真的不想为了执行这样的基本测试而开始包装基本的 Android API...
Attempting to write my first Android-by-TDD app (I've written a few small Android apps without TDD, so am familiar with the environment), but I can't seem to get my head around how to write my first test.
The scenario:
I have an activity, TasksActivity, and a service, TasksService. I need to test that TasksActivity starts TasksService in its onStart() method.
The test I've written is this:
public class ServiceControlTest extends ActivityUnitTestCase<TasksActivity>{
public ServiceControlTest() {
super(TasksActivity.class);
}
public void testStartServiceOnInit () {
final AtomicBoolean serviceStarted = new AtomicBoolean(false);
setActivityContext(new MockContext() {
@Override
public ComponentName startService(Intent service) {
Log.v("mockcontext", "Start service: " + service.toUri(0));
if (service.getComponent().getClassName().equals (TasksService.class.getName()))
serviceStarted.set(true);
return service.getComponent();
}
});
startActivity(new Intent(), null, null);
assertTrue ("Service should have been started", serviceStarted.get());
}
}
In my onCreate() method in TasksActivity I have:
startService(new Intent(this, TasksService.class));
I have also tried
getBaseContext().startService(new Intent(this, TasksService.class));
But in neither case does my MockContext's startService method get called. Is there a way I can set up interception of this method? I'd really rather not have to start wrapping the basic Android APIs in order to perform such basic tests...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
总结一下与 Brian Dupuis 在评论中的对话,问题是
MockContext
没有提供测试工具正确调用onCreate()
所需的设施。从MockContext
切换到ContextWrapper
解决了这个问题。因此,工作测试如下所示:
Just to summarise the conversation with Brian Dupuis in comments, the problem was that
MockContext
doesn't provide facilities that are required by the test instrumentation in order to correctly callonCreate()
. Switch fromMockContext
toContextWrapper
solved this problem.The working test therefore looks like this: