Skip to content Skip to sidebar Skip to footer

Android Service Not Launched By Junit Test

I have this test class to test a remote service: public class CoreServiceBasicTest extends ServiceTestCase implements ServiceConnection { /** Tag for loggin

Solution 1:

The whole point of Android Test Project (test.apk) is to instrument the Application Project (app.apk) and unit-test the Android components (Activity, Service and etc) which are associated with Application Project, in another word, unit-testing Activity and Service that is created and manipulated inside app.apk.

You should not write your MessengerService implementation partially (Messenger, IncomingHandler and etc) second time inside ServiceTestCase implementation under Test project. MessengerService implementation only need to be written once in your Application project's CoreService.java file.

ServiceConnection is used for inter-communication between Activity and Service, as we use ServiceTestCase here (means unit-test service, communication with other components is out-of-scope hence not considered), we don't need a ServiceConnection implementation. The only thing ServiceConnection does is initialize a solid Messenger object so that we could use later, once service is properly created:

publicvoidonServiceConnected(ComponentName className, IBinder service) {
  // This is what we want, we will call this manually in our TestCase, after calling// ServiceTestCase.bindService() and return the IBinder, check out code sample below.
  mService = newMessenger(service);
}

Also note that you don't need to call ServiceTestCase.startService() in this case, as ServiceTestCase.bindService() will properly start the service (if it is not started yet) and return a IBinder object we need to use to initialize Messenger object later.

Say if your IncomingHandler.handleMessage() impelementation in CoreService.java look like this:

... ...

switch (msg.what) {
  case MSG_SAY_HELLO:
    msgReceived = true;
    break;

... ...

To test send message functions in ServiceTestCase:

... ...

IBindermessengerBinder=null;

@OverridepublicvoidsetUp()throws Exception {
  super.setUp();
  // Bind the service and get a IBinder:
  messengerBinder = bindService(newIntent(this.getContext(), CoreService.class));
  //log service starting
  Log.d(TAG, "Service started and bound");
}

publicvoidtestSendMessage() {
  // Use IBinder create your Messenger, exactly what we did in ServiceConnection callback method:Messengermessenger=newMessenger(messengerBinder);
  Messagemsg= Message.obtain(null, MessengerService.MSG_SAY_HELLO, 0, 0);
  messenger.send(msg);
  // Do some assert stuff here
  ... ...
}

... ...

If your want to test communication between Activity and Service, then ServiceTestCase is not suitable in this case. Consider using ActivityInstrumentationTestCase2 test the actual Activity (which bound to your CoreService, which gives you ability to indirectly test your Service functions.

Solution 2:

Just looking at the documentation for ServiceTestCase it says that the test framework delays starting the service until one of your test methods calls ServiceTestCase.startService() or ServiceTestCase.bindService().

Looking at your code you call ServiceTestCase.startService() in your setUp() method, not in a test method. This doesn't start the service yet. It is waiting for one of your test methods to call ServiceTestCase.startService() or ServiceTestCase.bindService().

Your test method testBindService() isn't calling ServiceTestCase.bindService(), instead it is calling Context.bindService() and failing. The test framework is still waiting, so that's why the service isn't started.

Have another look at the Lifecycle support discussion in the linked developer docs.

Post a Comment for "Android Service Not Launched By Junit Test"