3

I have a class which starts IntentService:

public class MyClass(){
    public void doStuff(){
       Intent intent = new Intent(context, MyService.class);
       intent.putExtra(KEY, stringExtra);
       context.startService(intent);
    }
}

Now I want to unit-test MyClass. I want to check if the intent to start my service was fired and that it has correct extras. Something like this:

public void testServiceStarted(){
    MyClass myClass = new MyClass();
    myClass.doStuff();

    // Assert MyService was stated and received arguments
}

Is this possible with Instrumentation or other frameworks?

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • possible duplicate of [How to check if a service is running in android?](http://stackoverflow.com/questions/600207/how-to-check-if-a-service-is-running-in-android) – Paul Burke Sep 22 '14 at 11:44
  • There are tons of these questions on SO. You obviously didn't even take a moment to do a Google search. Shame. – Paul Burke Sep 22 '14 at 11:45
  • 1
    @PaulBurke you might not have read the question thoroughly. I asked how to write an automated test which checks if service was started. I think this is different. Also I see a difference between "running" and "was started"/"intent to start a service was fired". –  Sep 22 '14 at 12:02
  • Wow! My mistake, completely missed the "unit-test" and just thought you meant test, as in "determine". Please forgive. Removing down-vote. – Paul Burke Sep 22 '14 at 12:07
  • SO is not letting me undo the down-vote for some reason. Telling me it's now locked unless the question is edited. :-( – Paul Burke Sep 22 '14 at 12:09
  • @PaulBurke no problem. I've also edit original question to clarify things. –  Sep 22 '14 at 12:25

2 Answers2

0

I dont know if this is what you are looking for, but this might be useful.

private boolean isMyServiceRunning(Class<?> serviceClass,Context context) {
    ActivityManager manager = (ActivityManager)context. getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(MySercice.service.getClassName())) {
            Log.i("Service already","running");
            return true;
        }
    }
    Log.i("Service not","running");
    return false;
}

As you can see you will need a Context variable.

Another possible aproach is to have a static boolean in your service starting as false then, when your IntentService starts, change it to true.

Hope it helps

zozelfelfo
  • 3,776
  • 2
  • 21
  • 35
0

Though this is an old post, I was thinking of answering this question that might help somebody.

I use Roboelectric for writing such unit tests and here is how you can check if a service has started with the right action or intents.

@Config(constants = BuildConfig.class)
@RunWith(RobolectricTestRunner.class)
public class MyServiceTest {

    @Test
    public void testMyServiceStartedWithRightActionAndIntent() {

        final Intent realIntent = ShadowApplication.getInstance().getNextStartedService();

        // Test if the service intent is not null
        Assert.assertNotNull("Obtained the my service intent", realIntent);

        // Test if the service intent has any extra with the maching name with MY_EXTRA
        Assert.assertTrue("My service does not have the right extra", realIntent.hasExtra(MY_EXTRA));

        // Test if the service has the right action.
        Assert.assertEquals("My Service does not have right action", realIntent.getAction(), SERVICE_ACTION);
    }
}

For the Roboelectric setup, please see this link.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98