title: Android service not launched by JUnit test
body: <p>I have this test class to test a remote service:</p>
  
  <pre><code>public class CoreServiceBasicTest extends ServiceTestCase&lt;CoreService&gt; 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);
      }
  }
  </code></pre>
  
  <p>The problem is that startService(service) in the setUp() method does not launch the service correctly. This is what the AVD shows:</p>
  
  <p><img src="https://i.stack.imgur.com/Om10J.png" alt="enter image description here"></p>
  
  <p>As you can see, the process is launched but the service is not. Then on <code>testBindService()</code>, <code>assertTrue(isBound)</code> fails.</p>
  
  <p>This doesn't happen if I launch the service from an Activity: </p>
  
  <pre><code>@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();
  }
  </code></pre>
  
  <p>Here the service is started correctly, as shown below.</p>
  
  <p><img src="https://i.stack.imgur.com/GpVya.png" alt="enter image description here"></p>
  
  <p>How can I start and bind to a remote service that uses Messenger to communicate with activities from an Android Test Project?</p>