简体   繁体   中英

Binding to a service from an instrumentation test case

I've got an instrumentation test case that is attempting to bind to a service in a separate application for the purposes of inter-process communication (using the Android Interface Definition Language) between the test runner and the separate app. Every time I call one of the Stub methods defined in the service class from the test case class I get a null-pointer exception. I notice that the onStartCommand method in my Service class is never called, indicating that binding never occurs. My main concern is: it is even possible to bind to a service in a different process from a test runner? If so, what am I missing in my implementation?

instrumentation tests and test runner are for unit testing and small-scope functional testing. They won't work right with a large-scale integration test such as you are doing. To test that an Activity binds to a Service using AIDL across processes, you'll need to code a sample Activity that exercises the Service.

Two ways you can do

  1. Add instrumentation code as part of target app (Service app).
  2. Create .aidl file from the target app (service app) and add .aidl files to client app (instrumentation app). then only you can able to bind the service using Stub like

    SampleService.Stub sampleService = new SampleService.Stub.asInterface((IBinder) boundService)

More details about creating .aild: https://developer.android.com/guide/components/aidl.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM