单元测试 Activity.startService() 调用?

发布于 2025-01-05 08:32:07 字数 1389 浏览 1 评论 0原文

尝试编写我的第一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追风人 2025-01-12 08:32:07

总结一下与 Brian Dupuis 在评论中的对话,问题是 MockContext 没有提供测试工具正确调用 onCreate() 所需的设施。从 MockContext 切换到 ContextWrapper 解决了这个问题。

因此,工作测试如下所示:

public void testStartServiceOnInit () {
    final AtomicBoolean serviceStarted = new AtomicBoolean(false);
    setActivityContext(new ContextWrapper(getInstrumentation().getTargetContext()) {
        @Override
        public ComponentName startService(Intent service) {
            Log.v("mockcontext", "Start service: " + service.toUri(0));
            if (service.getComponent().getClassName().equals ("net.meridiandigital.tasks.TasksService"))
                serviceStarted.set(true);
            return service.getComponent();
        }
    });
    startActivity(new Intent(), null, null);
    assertTrue ("Service should have been started", serviceStarted.get());
}

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 call onCreate(). Switch from MockContext to ContextWrapper solved this problem.

The working test therefore looks like this:

public void testStartServiceOnInit () {
    final AtomicBoolean serviceStarted = new AtomicBoolean(false);
    setActivityContext(new ContextWrapper(getInstrumentation().getTargetContext()) {
        @Override
        public ComponentName startService(Intent service) {
            Log.v("mockcontext", "Start service: " + service.toUri(0));
            if (service.getComponent().getClassName().equals ("net.meridiandigital.tasks.TasksService"))
                serviceStarted.set(true);
            return service.getComponent();
        }
    });
    startActivity(new Intent(), null, null);
    assertTrue ("Service should have been started", serviceStarted.get());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文