【问题标题】:Test that an Activity Started a Service测试一个 Activity 是否启动了一个服务
【发布时间】:2013-08-13 20:16:21
【问题描述】:

如何测试我的活动是否启动了具有特定意图的服务?

我的问题类似于Unit testing Activity.startService() call,但是我在使用ActivityUnitTestCase 时遇到了action bar problem

这是我想测试的示例:

@UiThreadTest
public void testAddSongToAvailableList() {
    ListView songList = (ListView) this.getActivity().findViewById(R.id.allSongsList);
    songList.getChildAt(0).findViewById(R.id.btnAddToAvailableList).performClick(); // should start my service

    //assert that my service was started and is doing stuff
}

【问题讨论】:

    标签: android unit-testing android-service android-testing


    【解决方案1】:

    将数据作为捆绑包传递给启动活动?

    您是简单地测试服务已启动,还是想具体了解哪个活动启动了该服务?

    【讨论】:

    • 理想情况下,我想测试服务是否收到了活动的意图。像 ActivityMonitor 这样的东西可以很好地工作,但我不知道有一个用于服务的。
    【解决方案2】:

    使用startService方法启动服务时,可以通过方法的返回值判断Activity是否启动。

    如果服务启动成功,则返回the ComponentName of the actual service that was started is returned; else if the service does not exist null is returned.

    更多详情:Android Documentation

    【讨论】:

    • 我正在对我的应用程序进行单元测试。基本上我正在测试 startService() 是否被调用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多