> Android在线手册 > Android测试教程(8):测试Service

Android 测试框架也提供对Service测试的支持,基本类为ServiceTestCase,因为Service类通常假定和它是和Client是分开使用的,因此你可以无需使用Instrumentation 来测试Service。

当你设计一个Service时,你应该考虑测试用例中如何检查Service的当前状态,比如你在onCreate ,onStartCommand 中启动一个Service,一般没有一个全局变量来表示Service是否成功,你可能需要自己定义一个全局变量用于测试用例中。

ServiceTestCase 中提供getService() 可以取得当前被测试的Service对象。

ServiceTestCase 为AndroidTestCase的子类,因此可以测试和Permission 相关的功能,并提供Mock的Application 和Context 对象为测试Service提供了一个隔离的测试环境。

后面提供具体例子。