Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8231217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:24:42+00:00 2026-06-07T17:24:42+00:00

I have this test class to test a remote service: public class CoreServiceBasicTest extends

  • 0

I have this test class to test 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 launch the service correctly. This is what the AVD shows:

enter image description here

As you can see, the process is launched but the service is not. Then on testBindService(), assertTrue(isBound) fails.

This doesn’t happen if I launch the service from an 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 is started correctly, as shown below.

enter image description here

How can I start and bind to a remote service that uses Messenger to communicate with activities from an Android Test Project?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:24:43+00:00Added an answer on June 7, 2026 at 5:24 pm

    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:

    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);
    }
    

    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:

    ... ...
    
    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
      ... ...
    }
    
    ... ...
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say i have this usercontrol public class test : UserControl { public int
I have this simple test project just to test the IncludeExceptionDetailInFaults behavior. public class
I have a class that defines its own enum like this: public class Test
I have a test sample about coherence lock-unlock mechanism like this: public class Test
say I have: class Test { public static int Hello = 5; } This
I have a WCF Service like this: [ServiceContract] public class SomeService { [WebInvoke(UriTemplate =
I have this JUnit test that I need help developing a Interface and Class
I have some test class TestKlass = (function() { function TestKlass(value) { this.value =
I have a Class(ChorNumbers) and in this class i have a Function that test
Suppose I have the following html: This a test of <code>some code</code>. <div class='highlight'>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.