Android服务测试

ale*_*par 5 service android automated-tests unit-testing

如何测试我的IBinder对象Service返回onBind

Sur*_*jid 3

它取决于您在上下文和服务之间使用的远程接口(在远程调用场景中)。例如你可以这样做:

IBinder service = this.bindService(new Intent(TestService.class.getName()));
assertNotNull(service);
assertTrue(service instanceof ITestServiceCall); //see if the service returns the correct interface
ITestServiceCall iTestServiceCall = ITestServiceCall.Stub.asInterface(service);
assertNotNull(iTestServiceCall);
iTestServiceCall.doSomething();
Run Code Online (Sandbox Code Playgroud)

ITestServiceCall 是您在 AIDL 文件 ( ITestServiceCall.aidl ) 中定义的接口。

但在此之前,您必须确保您的服务在onBind()上正确返回接口的存根。

我希望这能有所帮助。