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

  • SEARCH
  • Home
  • 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 8688221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:18:21+00:00 2026-06-12T23:18:21+00:00

Using Robolectric, how would one go about testing an IntentService that broadcasts intents as

  • 0

Using Robolectric, how would one go about testing an IntentService that broadcasts intents as a response?

Assuming the following class:

class MyService extends IntentService {
    @Override
    protected void onHandleIntent(Intent intent) {
        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("action"));
    }
}

In my test case, I’m attempting to do something like this:

@RunWith(RobolectricTestRunner.class)
public class MyServiceTest{
    @Test
    public void testPurchaseHappyPath() throws Exception {

        Context context = new Activity();

        // register broadcast receiver
        BroadcastReceiver br = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                // test logic to ensure that this is called
            }

        };
        context.registerReceiver(br, new IntentFilter("action"));

        // This doesn't work
        context.startService(new Intent(context, MyService.class));

    }

}

MyService is never started using this approach. I’m relatively new to Robolectric, so I’m probably missing something obvious. Is there some sort of binding I have to do before calling startService? I’ve verified that broadcasting works by just calling sendBroadcast on the context. Any ideas?

  • 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-12T23:18:23+00:00Added an answer on June 12, 2026 at 11:18 pm

    You can’t test the service initialization like you’re trying to do. When you create a new activity under Robolectric, the activity you get back is actually a ShadowActivity (kind of). That means when you call startService, the method that actually gets executed is this one, which just calls into ShadowApplication#startService. This is the contents of that method:

    @Implementation
    @Override
    public ComponentName startService(Intent intent) {
        startedServices.add(intent);
        return new ComponentName("some.service.package", "SomeServiceName-FIXME");
    }
    

    You’ll notice that it doesn’t actually try to start your service at all. It just notes that you attempted to start the service. This is useful for the case that some code under test should start the service.

    If you want to test the actual service, I think you need to simulate the service lifecycle for the initialization bit. Something like this might work:

    @RunWith(RobolectricTestRunner.class)
    public class MyServiceTest{
        @Test
        public void testPurchaseHappyPath() throws Exception {
    
            Intent startIntent = new Intent(Robolectric.application, MyService.class);
            MyService service = new MyService();
            service.onCreate();
            service.onStartCommand(startIntent, 0, 42);
    
            // TODO: test test test
    
            service.onDestroy();
        }
    }
    

    I’m not familiar with how Robolectric treats BroadcastReceivers, so I left it out.

    EDIT: It might make even more sense to do the service creation/destruction in JUnit @Before/@After methods, which would allow your test to only contain the onStartCommand and “test test test” bits.

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

Sidebar

Related Questions

@using(Html.BeginForm(About, User, FormMethod.Post , new { id=aboutme})) { <fieldset> <ul> <li> <label class=block>About me</label>
Using C#, I need a class called User that has a username, password, active
I'm using robolectric to test an activity that makes use of Google Analytics. Unfortunately,
I'm trying to test the onHandleIntent() method of an IntentService using Robolectric . I'm
Using System.Diagnostics.EventLog .NET type one can programmatically create logs into the Event Viewer application.
using Telerik.WinControls.Data; using Telerik.WinControls.UI.Export; namespace Directory { public partial class radForm : Form {
I am using Robolectric without using maven to build, meaning I am using the
Using mercurial, I've run into an odd problem where a line from one committer
I'm using robolectric to make my android unit tests fast enough to be useful.
I have some unit tests (although they're Android tests, I'm using Robolectric , so

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.