【发布时间】:2014-05-10 11:27:02
【问题描述】:
我目前正在尝试使用 TDD 编写一个 Android 应用程序。我的任务是编写一个在应用程序中非常重要的服务。
出于这个原因,我正在尝试为该服务编写一个适当的测试。 Android 指南规定如下:
测试内容主题列出了测试 Android 组件的一般注意事项。以下是测试服务的一些具体指南:
确保调用 onCreate() 以响应 Context.startService() 或 Context.bindService()。同样,您应该确保调用 onDestroy() 以响应 Context.stopService()、Context.unbindService()、stopSelf() 或 stopSelfResult()。 测试您的服务是否正确处理来自 Context.startService() 的多个调用。只有第一次调用会触发 Service.onCreate(),但所有调用都会触发对 Service.onStartCommand() 的调用。
此外,请记住 startService() 调用不会嵌套,因此对 Context.stopService() 或 Service.stopSelf()(但不是 stopSelf(int))的单个调用将停止服务。您应该测试您的服务是否在正确的点停止。
测试您的服务实现的任何业务逻辑。业务逻辑包括检查无效值、财务和算术计算等。
我还没有看到对这些生命周期方法、多次调用 Context.startService() 等进行适当的测试。我正在尝试解决这个问题,但我目前不知所措。
我正在尝试使用 ServiceTestCase 类测试服务:
import java.util.List;
import CoreManagerService;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Before;
import org.junit.Test;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.test.ServiceTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;
/**
*
* This test should be executed on an actual device as recommended in the testing fundamentals.
* http://developer.android.com/tools/testing/testing_android.html#WhatToTest
*
* The following page describes tests that should be written for a service.
* http://developer.android.com/tools/testing/service_testing.html
* TODO: Write tests that check the proper execution of the service's life cycle.
*
*/
public class CoreManagerTest extends ServiceTestCase<CoreManagerService> {
/** Tag for logging */
private final static String TAG = CoreManagerTest.class.getName();
public CoreManagerTest () {
super(CoreManagerService.class);
}
public CoreManagerTest(Class<CoreManagerService> serviceClass) {
super(serviceClass);
// If not provided, then the ServiceTestCase will create it's own mock
// Context.
// setContext();
// The same goes for Application.
// setApplication();
Log.d(TAG, "Start of the Service test.");
}
@SmallTest
public void testPreConditions() {
}
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
super.setUp();
}
@After
public void tearDown() throws Exception {
super.tearDown();
}
@Test
public void testStartingService() {
getSystemContext().startService(new Intent(getSystemContext(), CoreManagerService.class));
isServiceRunning();
}
private void isServiceRunning() {
final ActivityManager activityManager = (ActivityManager)this.getSystemContext()
.getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningServiceInfo> services = activityManager
.getRunningServices(Integer.MAX_VALUE);
boolean serviceFound = false;
for (RunningServiceInfo runningServiceInfo : services) {
if (runningServiceInfo.service.getClassName().equals(
CoreManagerService.class.toString())) {
serviceFound = true;
}
}
assertTrue(serviceFound);
}
}
我是不是在错误地处理这个问题?我应该使用活动测试来测试服务的绑定吗?
【问题讨论】:
-
这有什么更新吗?我一直在寻找类似的答案,但没有找到任何有用的东西。
-
我已经有一段时间没有问这个问题了,我想我可能可以编写一个单元测试来测试生命周期方法。您可能可以使用广播(context.sendBroadcast() 和广播接收器)将消息从服务发送到测试。我也许可以为你写一个,不过我得看看什么时候有时间。
-
好吧,我有一个粘性远程服务,它运行另一个线程来执行异步任务。测试生命周期会很好,但我真正需要的是测试线程处理程序。我没有看到太多的例子。我正在考虑为我的线程创建一个存根或使用模拟对象,但我不知道如何在服务或状态更改中测试非生命周期方法。有什么想法吗?
标签: android unit-testing service junit4