Skip to content

how to test IntentService

b1uebyte edited this page Feb 20, 2013 · 2 revisions

A way to test the protected onHandleIntent() method of an IntentService is to extend the IntentService class under test and override onHandleIntent(), making it public and calling super(intent).

then simply invoke onHandleIntent of the new class in the test case.

in order to test the incoming intent for right parameters use getNextStartedService of a shadow Activity and do your assertion on the returned intent.

class MyServiceTestable extends MyService {
		@Override
		public void onHandleIntent(Intent intent) {
			// TODO Auto-generated method stub
			super.onHandleIntent(intent);
		}
};```

@Test
public void myMethod__shouldStartServiceWithCorrectParameters() throws Exception {
		myActvitity.MyMathod();
		ShadowActivity shadowActivity = Robolectric.shadowOf(myActivity);
		Intent intent = shadowActivity.getNextStartedService();
}