Android service does not start using JUnit test

I have this test class for testing a remote service:

public class CoreServiceBasicTest extends ServiceTestCase<CoreService> implements ServiceConnection {

    /** Tag for logging */
    private final static String TAG = CoreServiceBasicTest.class.getName();

    /** Receive incoming messages */
    private final Messenger inMessenger = new Messenger(new IncomingHandler());

    /** Communicate with the service */
    private Messenger outMessenger = null;

    /** Handler of incoming messages from service */
    private static class IncomingHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            Log.d(TAG, "Incoming message");
        }
    }

    /** Constructor for service test */
    public CoreServiceBasicTest() {
        super(CoreService.class);
    }

    /** Start the service */
    @Override
    public void setUp() {

        // Mandatory
        try {
            super.setUp();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Start the service
        Intent service = new Intent();
        service.setClass(this.getContext(), CoreService.class);
        startService(service);
        Log.d(TAG, "Service started");
    }

    public void onServiceConnected(ComponentName className, IBinder service) {
        outMessenger = new Messenger(service);
        Log.d(TAG, "Service attached");
    }

    public void onServiceDisconnected(ComponentName className) {
        // TODO Auto-generated method stub

    }

    @SmallTest
    public void testBindService() {
        // Bind to the service
        Intent service = new Intent();
        service.setClass(getContext(), CoreService.class);
        boolean isBound = getContext().bindService(service, this, Context.BIND_AUTO_CREATE);
        assertTrue(isBound);
    }
}

The problem is that startService (service) in the setUp () method does not start the service correctly. This shows the AVD:

enter image description here

As you can see, the process starts, but there is no service. Then on testBindService(), assertTrue(isBound)fails.

This does not happen if I start the service from Activity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Start the Core service
    Intent service = new Intent();
    service.setClass(this, CoreService.class);

    if (startService(service) == null) {
        Toast.makeText(this, "Error starting service!", Toast.LENGTH_LONG).show();
        Log.e(TAG, "Error starting service");
    } else {
        Toast.makeText(this, "Service started sucessfully", Toast.LENGTH_LONG).show();
    }

    // Die
    finish();
}

Here the service starts correctly, as shown below.

enter image description here

How can I start and bind to a remote service that uses Messenger to communicate with actions in an Android test project?

+5
source share
2 answers

Android Test Project (test.apk) - Application Project (app.apk) unit Android (Activity, Service ..), Application Project, , unit-testing Activity Service, app.apk.

MessengerService (Messenger, IncomingHandler ..) ServiceTestCase Test. MessengerService CoreService.java .

ServiceConnection Activity Service, ServiceTestCase (, unit-test, , ), ServiceConnection, , ServiceConnection, Messenger, , :

public void onServiceConnected(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 = new Messenger(service);
}

, ServiceTestCase.startService(), ServiceTestCase.bindService() ( ) IBinder, Messenger .

, ImcomingHandler.handleMessage() impelementation CoreService.java :

... ...

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

... ...

ServiceTestCase:

... ...

IBinder messengerBinder = null;

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

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

... ...

Activity Service, ServiceTestCase . , ActivityInstrumentationTestCase2 ( CoreService, .

+11

ServiceTestCase, , , ServiceTestCase.startService() ServiceTestCase.bindService().

, ServiceTestCase.startService() setUp(), . . , ServiceTestCase.startService() ServiceTestCase.bindService().

testBindService() ServiceTestCase.bindService(), Context.bindService() . , .

Lifecycle support .

+7

All Articles