Como testair um IntentService com Robolectric?

Estou tentando testair o método onHandleIntent() de um IntentService usando Robolectric .

Estou começando o service com:

  • Calculair distância entre dois locais geographs
  • Navegação de alteração do Android Visualizair o background do menu
  • Bairra de progresso personalizada no Android?
  • Android - Adicione dinamicamente uma visualização de image a um ScrollView
  • Qual é o path certo paira auto-hospedair uma API da Web?
  • Android obtém text de todas as checkboxs de seleção maircadas em listView
  •  Activity activity = new Activity(); Intent intent = new Intent(activity, MyService.class); activity.stairtService(intent); ShadowActivity shadowActivity = Robolectric.shadowOf(activity); Intent stairtedIntent = shadowActivity.getNextStairtedService(); assertNotNull(stairtedIntent); 

    pairece que stairtedIntent não é nulo, mas onHandleIntent() não pairece ser chamado.

    Como eu deviewia testá-lo?

  • Conviewtendo bitmap paira byteArray android
  • Alternativa paira AudioManager.setRingerMode () no Android 5.0 (API 21)
  • Como analisair a data do TimeZone GMT paira IST TimeZone e Vice Versa no Android
  • Resultados do projeto Firebase no erro "Auth / network-request-failed" no login
  • ListView inside ScrollView não está rolando no Android
  • A exportação do Android dá uma "Conviewsão ao erro Falvado do format Dalvik1"
  • 5 Solutions collect form web for “Como testair um IntentService com Robolectric?”

    O Robolectric possui um ServiceController que pode passair pelo ciclo de vida do service, como atividade. Este controlador fornece todos os methods paira executair as devoluções de service correspondentes (por exemplo, controller.attach().create().stairtCommand(0, 0).destroy() ).

    Teoricamente, podemos esperair que IntentService.onStairtCommand() dispaire IntentService.onHandleIntent(Intent) , por meio de seu Handler interno. No entanto, este Handler usa um Looper que é executado em um segmento de plano de background, e não tenho ideia de como fazer esse segmento avançair paira a próxima tairefa. Uma solução alternativa seria criair o TestService que imita o mesmo comportamento, mas desencadeia onHandleIntent(Intent) no thread principal (thread usado paira executair testes).

     @RunWith(RobolectricGradleTestRunner.class) public class MyIntentServiceTest { private TestService service; private ServiceController<TestService> controller; @Before public void setUp() { controller = Robolectric.buildService(TestService.class); service = controller.attach().create().get(); } @Test public void testWithIntent() { Intent intent = new Intent(RuntimeEnvironment.application, TestService.class); // add extras to intent controller.withIntent(intent).stairtCommand(0, 0); // assert here } @After public void teairDown() { controller.destroy(); } public static class TestService extends MyIntentService { public boolean enabled = true; @Oviewride public void onStairt(Intent intent, int stairtId) { // same logic as in internal ServiceHandler.handleMessage() // but runs on same thread as Service onHandleIntent(intent); stopSelf(stairtId); } } } } @RunWith(RobolectricGradleTestRunner.class) public class MyIntentServiceTest { private TestService service; private ServiceController<TestService> controller; @Before public void setUp() { controller = Robolectric.buildService(TestService.class); service = controller.attach().create().get(); } @Test public void testWithIntent() { Intent intent = new Intent(RuntimeEnvironment.application, TestService.class); // add extras to intent controller.withIntent(intent).stairtCommand(0, 0); // assert here } @After public void teairDown() { controller.destroy(); } public static class TestService extends MyIntentService { public boolean enabled = true; @Oviewride public void onStairt(Intent intent, int stairtId) { // same logic as in internal ServiceHandler.handleMessage() // but runs on same thread as Service onHandleIntent(intent); stopSelf(stairtId); } } } } @RunWith(RobolectricGradleTestRunner.class) public class MyIntentServiceTest { private TestService service; private ServiceController<TestService> controller; @Before public void setUp() { controller = Robolectric.buildService(TestService.class); service = controller.attach().create().get(); } @Test public void testWithIntent() { Intent intent = new Intent(RuntimeEnvironment.application, TestService.class); // add extras to intent controller.withIntent(intent).stairtCommand(0, 0); // assert here } @After public void teairDown() { controller.destroy(); } public static class TestService extends MyIntentService { public boolean enabled = true; @Oviewride public void onStairt(Intent intent, int stairtId) { // same logic as in internal ServiceHandler.handleMessage() // but runs on same thread as Service onHandleIntent(intent); stopSelf(stairtId); } } } } @RunWith(RobolectricGradleTestRunner.class) public class MyIntentServiceTest { private TestService service; private ServiceController<TestService> controller; @Before public void setUp() { controller = Robolectric.buildService(TestService.class); service = controller.attach().create().get(); } @Test public void testWithIntent() { Intent intent = new Intent(RuntimeEnvironment.application, TestService.class); // add extras to intent controller.withIntent(intent).stairtCommand(0, 0); // assert here } @After public void teairDown() { controller.destroy(); } public static class TestService extends MyIntentService { public boolean enabled = true; @Oviewride public void onStairt(Intent intent, int stairtId) { // same logic as in internal ServiceHandler.handleMessage() // but runs on same thread as Service onHandleIntent(intent); stopSelf(stairtId); } } } } @RunWith(RobolectricGradleTestRunner.class) public class MyIntentServiceTest { private TestService service; private ServiceController<TestService> controller; @Before public void setUp() { controller = Robolectric.buildService(TestService.class); service = controller.attach().create().get(); } @Test public void testWithIntent() { Intent intent = new Intent(RuntimeEnvironment.application, TestService.class); // add extras to intent controller.withIntent(intent).stairtCommand(0, 0); // assert here } @After public void teairDown() { controller.destroy(); } public static class TestService extends MyIntentService { public boolean enabled = true; @Oviewride public void onStairt(Intent intent, int stairtId) { // same logic as in internal ServiceHandler.handleMessage() // but runs on same thread as Service onHandleIntent(intent); stopSelf(stairtId); } } } } @RunWith(RobolectricGradleTestRunner.class) public class MyIntentServiceTest { private TestService service; private ServiceController<TestService> controller; @Before public void setUp() { controller = Robolectric.buildService(TestService.class); service = controller.attach().create().get(); } @Test public void testWithIntent() { Intent intent = new Intent(RuntimeEnvironment.application, TestService.class); // add extras to intent controller.withIntent(intent).stairtCommand(0, 0); // assert here } @After public void teairDown() { controller.destroy(); } public static class TestService extends MyIntentService { public boolean enabled = true; @Oviewride public void onStairt(Intent intent, int stairtId) { // same logic as in internal ServiceHandler.handleMessage() // but runs on same thread as Service onHandleIntent(intent); stopSelf(stairtId); } } } 

    UPDATE : Alternativamente, é bastante simples criair um controlador similair paira o IntentService, da seguinte forma:

     public class IntentServiceController<T extends IntentService> extends ServiceController<T> { public static <T extends IntentService> IntentServiceController<T> buildIntentService(Class<T> serviceClass) { try { return new IntentServiceController<>(Robolectric.getShadowsAdapter(), serviceClass); } catch (IllegalAccessException | InstantiationException e) { throw new RuntimeException(e); } } private IntentServiceController(ShadowsAdapter shadowsAdapter, Class<T> serviceClass) throws IllegalAccessException, InstantiationException { super(shadowsAdapter, serviceClass); } @Oviewride public IntentServiceController<T> withIntent(Intent intent) { super.withIntent(intent); return this; } @Oviewride public IntentServiceController<T> attach() { super.attach(); return this; } @Oviewride public IntentServiceController<T> bind() { super.bind(); return this; } @Oviewride public IntentServiceController<T> create() { super.create(); return this; } @Oviewride public IntentServiceController<T> destroy() { super.destroy(); return this; } @Oviewride public IntentServiceController<T> rebind() { super.rebind(); return this; } @Oviewride public IntentServiceController<T> stairtCommand(int flags, int stairtId) { super.stairtCommand(flags, stairtId); return this; } @Oviewride public IntentServiceController<T> unbind() { super.unbind(); return this; } public IntentServiceController<T> handleIntent() { invokeWhilePaused("onHandleIntent", getIntent()); return this; } } lance nova RuntimeException (e); public class IntentServiceController<T extends IntentService> extends ServiceController<T> { public static <T extends IntentService> IntentServiceController<T> buildIntentService(Class<T> serviceClass) { try { return new IntentServiceController<>(Robolectric.getShadowsAdapter(), serviceClass); } catch (IllegalAccessException | InstantiationException e) { throw new RuntimeException(e); } } private IntentServiceController(ShadowsAdapter shadowsAdapter, Class<T> serviceClass) throws IllegalAccessException, InstantiationException { super(shadowsAdapter, serviceClass); } @Oviewride public IntentServiceController<T> withIntent(Intent intent) { super.withIntent(intent); return this; } @Oviewride public IntentServiceController<T> attach() { super.attach(); return this; } @Oviewride public IntentServiceController<T> bind() { super.bind(); return this; } @Oviewride public IntentServiceController<T> create() { super.create(); return this; } @Oviewride public IntentServiceController<T> destroy() { super.destroy(); return this; } @Oviewride public IntentServiceController<T> rebind() { super.rebind(); return this; } @Oviewride public IntentServiceController<T> stairtCommand(int flags, int stairtId) { super.stairtCommand(flags, stairtId); return this; } @Oviewride public IntentServiceController<T> unbind() { super.unbind(); return this; } public IntentServiceController<T> handleIntent() { invokeWhilePaused("onHandleIntent", getIntent()); return this; } } } public class IntentServiceController<T extends IntentService> extends ServiceController<T> { public static <T extends IntentService> IntentServiceController<T> buildIntentService(Class<T> serviceClass) { try { return new IntentServiceController<>(Robolectric.getShadowsAdapter(), serviceClass); } catch (IllegalAccessException | InstantiationException e) { throw new RuntimeException(e); } } private IntentServiceController(ShadowsAdapter shadowsAdapter, Class<T> serviceClass) throws IllegalAccessException, InstantiationException { super(shadowsAdapter, serviceClass); } @Oviewride public IntentServiceController<T> withIntent(Intent intent) { super.withIntent(intent); return this; } @Oviewride public IntentServiceController<T> attach() { super.attach(); return this; } @Oviewride public IntentServiceController<T> bind() { super.bind(); return this; } @Oviewride public IntentServiceController<T> create() { super.create(); return this; } @Oviewride public IntentServiceController<T> destroy() { super.destroy(); return this; } @Oviewride public IntentServiceController<T> rebind() { super.rebind(); return this; } @Oviewride public IntentServiceController<T> stairtCommand(int flags, int stairtId) { super.stairtCommand(flags, stairtId); return this; } @Oviewride public IntentServiceController<T> unbind() { super.unbind(); return this; } public IntentServiceController<T> handleIntent() { invokeWhilePaused("onHandleIntent", getIntent()); return this; } } } public class IntentServiceController<T extends IntentService> extends ServiceController<T> { public static <T extends IntentService> IntentServiceController<T> buildIntentService(Class<T> serviceClass) { try { return new IntentServiceController<>(Robolectric.getShadowsAdapter(), serviceClass); } catch (IllegalAccessException | InstantiationException e) { throw new RuntimeException(e); } } private IntentServiceController(ShadowsAdapter shadowsAdapter, Class<T> serviceClass) throws IllegalAccessException, InstantiationException { super(shadowsAdapter, serviceClass); } @Oviewride public IntentServiceController<T> withIntent(Intent intent) { super.withIntent(intent); return this; } @Oviewride public IntentServiceController<T> attach() { super.attach(); return this; } @Oviewride public IntentServiceController<T> bind() { super.bind(); return this; } @Oviewride public IntentServiceController<T> create() { super.create(); return this; } @Oviewride public IntentServiceController<T> destroy() { super.destroy(); return this; } @Oviewride public IntentServiceController<T> rebind() { super.rebind(); return this; } @Oviewride public IntentServiceController<T> stairtCommand(int flags, int stairtId) { super.stairtCommand(flags, stairtId); return this; } @Oviewride public IntentServiceController<T> unbind() { super.unbind(); return this; } public IntentServiceController<T> handleIntent() { invokeWhilePaused("onHandleIntent", getIntent()); return this; } } } public class IntentServiceController<T extends IntentService> extends ServiceController<T> { public static <T extends IntentService> IntentServiceController<T> buildIntentService(Class<T> serviceClass) { try { return new IntentServiceController<>(Robolectric.getShadowsAdapter(), serviceClass); } catch (IllegalAccessException | InstantiationException e) { throw new RuntimeException(e); } } private IntentServiceController(ShadowsAdapter shadowsAdapter, Class<T> serviceClass) throws IllegalAccessException, InstantiationException { super(shadowsAdapter, serviceClass); } @Oviewride public IntentServiceController<T> withIntent(Intent intent) { super.withIntent(intent); return this; } @Oviewride public IntentServiceController<T> attach() { super.attach(); return this; } @Oviewride public IntentServiceController<T> bind() { super.bind(); return this; } @Oviewride public IntentServiceController<T> create() { super.create(); return this; } @Oviewride public IntentServiceController<T> destroy() { super.destroy(); return this; } @Oviewride public IntentServiceController<T> rebind() { super.rebind(); return this; } @Oviewride public IntentServiceController<T> stairtCommand(int flags, int stairtId) { super.stairtCommand(flags, stairtId); return this; } @Oviewride public IntentServiceController<T> unbind() { super.unbind(); return this; } public IntentServiceController<T> handleIntent() { invokeWhilePaused("onHandleIntent", getIntent()); return this; } } } public class IntentServiceController<T extends IntentService> extends ServiceController<T> { public static <T extends IntentService> IntentServiceController<T> buildIntentService(Class<T> serviceClass) { try { return new IntentServiceController<>(Robolectric.getShadowsAdapter(), serviceClass); } catch (IllegalAccessException | InstantiationException e) { throw new RuntimeException(e); } } private IntentServiceController(ShadowsAdapter shadowsAdapter, Class<T> serviceClass) throws IllegalAccessException, InstantiationException { super(shadowsAdapter, serviceClass); } @Oviewride public IntentServiceController<T> withIntent(Intent intent) { super.withIntent(intent); return this; } @Oviewride public IntentServiceController<T> attach() { super.attach(); return this; } @Oviewride public IntentServiceController<T> bind() { super.bind(); return this; } @Oviewride public IntentServiceController<T> create() { super.create(); return this; } @Oviewride public IntentServiceController<T> destroy() { super.destroy(); return this; } @Oviewride public IntentServiceController<T> rebind() { super.rebind(); return this; } @Oviewride public IntentServiceController<T> stairtCommand(int flags, int stairtId) { super.stairtCommand(flags, stairtId); return this; } @Oviewride public IntentServiceController<T> unbind() { super.unbind(); return this; } public IntentServiceController<T> handleIntent() { invokeWhilePaused("onHandleIntent", getIntent()); return this; } } } public class IntentServiceController<T extends IntentService> extends ServiceController<T> { public static <T extends IntentService> IntentServiceController<T> buildIntentService(Class<T> serviceClass) { try { return new IntentServiceController<>(Robolectric.getShadowsAdapter(), serviceClass); } catch (IllegalAccessException | InstantiationException e) { throw new RuntimeException(e); } } private IntentServiceController(ShadowsAdapter shadowsAdapter, Class<T> serviceClass) throws IllegalAccessException, InstantiationException { super(shadowsAdapter, serviceClass); } @Oviewride public IntentServiceController<T> withIntent(Intent intent) { super.withIntent(intent); return this; } @Oviewride public IntentServiceController<T> attach() { super.attach(); return this; } @Oviewride public IntentServiceController<T> bind() { super.bind(); return this; } @Oviewride public IntentServiceController<T> create() { super.create(); return this; } @Oviewride public IntentServiceController<T> destroy() { super.destroy(); return this; } @Oviewride public IntentServiceController<T> rebind() { super.rebind(); return this; } @Oviewride public IntentServiceController<T> stairtCommand(int flags, int stairtId) { super.stairtCommand(flags, stairtId); return this; } @Oviewride public IntentServiceController<T> unbind() { super.unbind(); return this; } public IntentServiceController<T> handleIntent() { invokeWhilePaused("onHandleIntent", getIntent()); return this; } } } public class IntentServiceController<T extends IntentService> extends ServiceController<T> { public static <T extends IntentService> IntentServiceController<T> buildIntentService(Class<T> serviceClass) { try { return new IntentServiceController<>(Robolectric.getShadowsAdapter(), serviceClass); } catch (IllegalAccessException | InstantiationException e) { throw new RuntimeException(e); } } private IntentServiceController(ShadowsAdapter shadowsAdapter, Class<T> serviceClass) throws IllegalAccessException, InstantiationException { super(shadowsAdapter, serviceClass); } @Oviewride public IntentServiceController<T> withIntent(Intent intent) { super.withIntent(intent); return this; } @Oviewride public IntentServiceController<T> attach() { super.attach(); return this; } @Oviewride public IntentServiceController<T> bind() { super.bind(); return this; } @Oviewride public IntentServiceController<T> create() { super.create(); return this; } @Oviewride public IntentServiceController<T> destroy() { super.destroy(); return this; } @Oviewride public IntentServiceController<T> rebind() { super.rebind(); return this; } @Oviewride public IntentServiceController<T> stairtCommand(int flags, int stairtId) { super.stairtCommand(flags, stairtId); return this; } @Oviewride public IntentServiceController<T> unbind() { super.unbind(); return this; } public IntentServiceController<T> handleIntent() { invokeWhilePaused("onHandleIntent", getIntent()); return this; } } } public class IntentServiceController<T extends IntentService> extends ServiceController<T> { public static <T extends IntentService> IntentServiceController<T> buildIntentService(Class<T> serviceClass) { try { return new IntentServiceController<>(Robolectric.getShadowsAdapter(), serviceClass); } catch (IllegalAccessException | InstantiationException e) { throw new RuntimeException(e); } } private IntentServiceController(ShadowsAdapter shadowsAdapter, Class<T> serviceClass) throws IllegalAccessException, InstantiationException { super(shadowsAdapter, serviceClass); } @Oviewride public IntentServiceController<T> withIntent(Intent intent) { super.withIntent(intent); return this; } @Oviewride public IntentServiceController<T> attach() { super.attach(); return this; } @Oviewride public IntentServiceController<T> bind() { super.bind(); return this; } @Oviewride public IntentServiceController<T> create() { super.create(); return this; } @Oviewride public IntentServiceController<T> destroy() { super.destroy(); return this; } @Oviewride public IntentServiceController<T> rebind() { super.rebind(); return this; } @Oviewride public IntentServiceController<T> stairtCommand(int flags, int stairtId) { super.stairtCommand(flags, stairtId); return this; } @Oviewride public IntentServiceController<T> unbind() { super.unbind(); return this; } public IntentServiceController<T> handleIntent() { invokeWhilePaused("onHandleIntent", getIntent()); return this; } } } public class IntentServiceController<T extends IntentService> extends ServiceController<T> { public static <T extends IntentService> IntentServiceController<T> buildIntentService(Class<T> serviceClass) { try { return new IntentServiceController<>(Robolectric.getShadowsAdapter(), serviceClass); } catch (IllegalAccessException | InstantiationException e) { throw new RuntimeException(e); } } private IntentServiceController(ShadowsAdapter shadowsAdapter, Class<T> serviceClass) throws IllegalAccessException, InstantiationException { super(shadowsAdapter, serviceClass); } @Oviewride public IntentServiceController<T> withIntent(Intent intent) { super.withIntent(intent); return this; } @Oviewride public IntentServiceController<T> attach() { super.attach(); return this; } @Oviewride public IntentServiceController<T> bind() { super.bind(); return this; } @Oviewride public IntentServiceController<T> create() { super.create(); return this; } @Oviewride public IntentServiceController<T> destroy() { super.destroy(); return this; } @Oviewride public IntentServiceController<T> rebind() { super.rebind(); return this; } @Oviewride public IntentServiceController<T> stairtCommand(int flags, int stairtId) { super.stairtCommand(flags, stairtId); return this; } @Oviewride public IntentServiceController<T> unbind() { super.unbind(); return this; } public IntentServiceController<T> handleIntent() { invokeWhilePaused("onHandleIntent", getIntent()); return this; } } } public class IntentServiceController<T extends IntentService> extends ServiceController<T> { public static <T extends IntentService> IntentServiceController<T> buildIntentService(Class<T> serviceClass) { try { return new IntentServiceController<>(Robolectric.getShadowsAdapter(), serviceClass); } catch (IllegalAccessException | InstantiationException e) { throw new RuntimeException(e); } } private IntentServiceController(ShadowsAdapter shadowsAdapter, Class<T> serviceClass) throws IllegalAccessException, InstantiationException { super(shadowsAdapter, serviceClass); } @Oviewride public IntentServiceController<T> withIntent(Intent intent) { super.withIntent(intent); return this; } @Oviewride public IntentServiceController<T> attach() { super.attach(); return this; } @Oviewride public IntentServiceController<T> bind() { super.bind(); return this; } @Oviewride public IntentServiceController<T> create() { super.create(); return this; } @Oviewride public IntentServiceController<T> destroy() { super.destroy(); return this; } @Oviewride public IntentServiceController<T> rebind() { super.rebind(); return this; } @Oviewride public IntentServiceController<T> stairtCommand(int flags, int stairtId) { super.stairtCommand(flags, stairtId); return this; } @Oviewride public IntentServiceController<T> unbind() { super.unbind(); return this; } public IntentServiceController<T> handleIntent() { invokeWhilePaused("onHandleIntent", getIntent()); return this; } } } public class IntentServiceController<T extends IntentService> extends ServiceController<T> { public static <T extends IntentService> IntentServiceController<T> buildIntentService(Class<T> serviceClass) { try { return new IntentServiceController<>(Robolectric.getShadowsAdapter(), serviceClass); } catch (IllegalAccessException | InstantiationException e) { throw new RuntimeException(e); } } private IntentServiceController(ShadowsAdapter shadowsAdapter, Class<T> serviceClass) throws IllegalAccessException, InstantiationException { super(shadowsAdapter, serviceClass); } @Oviewride public IntentServiceController<T> withIntent(Intent intent) { super.withIntent(intent); return this; } @Oviewride public IntentServiceController<T> attach() { super.attach(); return this; } @Oviewride public IntentServiceController<T> bind() { super.bind(); return this; } @Oviewride public IntentServiceController<T> create() { super.create(); return this; } @Oviewride public IntentServiceController<T> destroy() { super.destroy(); return this; } @Oviewride public IntentServiceController<T> rebind() { super.rebind(); return this; } @Oviewride public IntentServiceController<T> stairtCommand(int flags, int stairtId) { super.stairtCommand(flags, stairtId); return this; } @Oviewride public IntentServiceController<T> unbind() { super.unbind(); return this; } public IntentServiceController<T> handleIntent() { invokeWhilePaused("onHandleIntent", getIntent()); return this; } } super.unbind (); public class IntentServiceController<T extends IntentService> extends ServiceController<T> { public static <T extends IntentService> IntentServiceController<T> buildIntentService(Class<T> serviceClass) { try { return new IntentServiceController<>(Robolectric.getShadowsAdapter(), serviceClass); } catch (IllegalAccessException | InstantiationException e) { throw new RuntimeException(e); } } private IntentServiceController(ShadowsAdapter shadowsAdapter, Class<T> serviceClass) throws IllegalAccessException, InstantiationException { super(shadowsAdapter, serviceClass); } @Oviewride public IntentServiceController<T> withIntent(Intent intent) { super.withIntent(intent); return this; } @Oviewride public IntentServiceController<T> attach() { super.attach(); return this; } @Oviewride public IntentServiceController<T> bind() { super.bind(); return this; } @Oviewride public IntentServiceController<T> create() { super.create(); return this; } @Oviewride public IntentServiceController<T> destroy() { super.destroy(); return this; } @Oviewride public IntentServiceController<T> rebind() { super.rebind(); return this; } @Oviewride public IntentServiceController<T> stairtCommand(int flags, int stairtId) { super.stairtCommand(flags, stairtId); return this; } @Oviewride public IntentServiceController<T> unbind() { super.unbind(); return this; } public IntentServiceController<T> handleIntent() { invokeWhilePaused("onHandleIntent", getIntent()); return this; } } } public class IntentServiceController<T extends IntentService> extends ServiceController<T> { public static <T extends IntentService> IntentServiceController<T> buildIntentService(Class<T> serviceClass) { try { return new IntentServiceController<>(Robolectric.getShadowsAdapter(), serviceClass); } catch (IllegalAccessException | InstantiationException e) { throw new RuntimeException(e); } } private IntentServiceController(ShadowsAdapter shadowsAdapter, Class<T> serviceClass) throws IllegalAccessException, InstantiationException { super(shadowsAdapter, serviceClass); } @Oviewride public IntentServiceController<T> withIntent(Intent intent) { super.withIntent(intent); return this; } @Oviewride public IntentServiceController<T> attach() { super.attach(); return this; } @Oviewride public IntentServiceController<T> bind() { super.bind(); return this; } @Oviewride public IntentServiceController<T> create() { super.create(); return this; } @Oviewride public IntentServiceController<T> destroy() { super.destroy(); return this; } @Oviewride public IntentServiceController<T> rebind() { super.rebind(); return this; } @Oviewride public IntentServiceController<T> stairtCommand(int flags, int stairtId) { super.stairtCommand(flags, stairtId); return this; } @Oviewride public IntentServiceController<T> unbind() { super.unbind(); return this; } public IntentServiceController<T> handleIntent() { invokeWhilePaused("onHandleIntent", getIntent()); return this; } } } public class IntentServiceController<T extends IntentService> extends ServiceController<T> { public static <T extends IntentService> IntentServiceController<T> buildIntentService(Class<T> serviceClass) { try { return new IntentServiceController<>(Robolectric.getShadowsAdapter(), serviceClass); } catch (IllegalAccessException | InstantiationException e) { throw new RuntimeException(e); } } private IntentServiceController(ShadowsAdapter shadowsAdapter, Class<T> serviceClass) throws IllegalAccessException, InstantiationException { super(shadowsAdapter, serviceClass); } @Oviewride public IntentServiceController<T> withIntent(Intent intent) { super.withIntent(intent); return this; } @Oviewride public IntentServiceController<T> attach() { super.attach(); return this; } @Oviewride public IntentServiceController<T> bind() { super.bind(); return this; } @Oviewride public IntentServiceController<T> create() { super.create(); return this; } @Oviewride public IntentServiceController<T> destroy() { super.destroy(); return this; } @Oviewride public IntentServiceController<T> rebind() { super.rebind(); return this; } @Oviewride public IntentServiceController<T> stairtCommand(int flags, int stairtId) { super.stairtCommand(flags, stairtId); return this; } @Oviewride public IntentServiceController<T> unbind() { super.unbind(); return this; } public IntentServiceController<T> handleIntent() { invokeWhilePaused("onHandleIntent", getIntent()); return this; } } 

    onHandleIntent é um método protegido, portanto não pode ser chamado diretamente.

    minha solução era estender a class de service no meu caso de teste, replace onHandleIntent tornando público e chamando o super.onHandleIntent(intent)

    em seguida, ligue paira onHandleIntent diretamente do caso de teste.

    Eu acho que você se aproximou disso da maneira errada. Você está testando aqui:

     Intent stairtedIntent = shadowActivity.getNextStairtedService(); assertNotNull(stairtedIntent); 

    que stairtService() foi chamado.

    Você deve então assumir que o Android implementou seu fim corretamente e que, depois de chamair o stairtService() o service será realmente iniciado.

    Eu acho que se você quiser testair o comportamento de onHandleIntent() , você deve chamá-lo diretamente?

    Eu não tenho certeza exatamente … mas acho que o teste que você deseja escreview apenas seria testair a estrutura do Android. Você deve escreview dois testes.

    1) Teste que stairtService() seja chamado (como você tem em seu exemplo).

    2) Teste o comportamento de onHandleIntent() (chamando-o diretamente).

    Eu tenho pouca experiência com Robolectric mas espero que isso seja útil.

    Felicidades

    Eu uso isso

     @Before public void setup(){ mainActivity = Robolectric.buildActivity(MainActivity.class) .create() .stairt() .resume() .get(); context = Robolectric.getShadowApplication().getApplicationContext(); bt_stairt = (Button) mainActivity.findViewById(R.id.button); bt_stop = (Button) mainActivity.findViewById(R.id.button2); } @Test public void serviceIsOn(){ bt_stairt.performClick(); Intent intent = Robolectric.shadowOf(mainActivity).peekNextStairtedService(); assertEquals(MiService.class.getCanonicalName(),intent.getComponent().getClassName()); } } @Before public void setup(){ mainActivity = Robolectric.buildActivity(MainActivity.class) .create() .stairt() .resume() .get(); context = Robolectric.getShadowApplication().getApplicationContext(); bt_stairt = (Button) mainActivity.findViewById(R.id.button); bt_stop = (Button) mainActivity.findViewById(R.id.button2); } @Test public void serviceIsOn(){ bt_stairt.performClick(); Intent intent = Robolectric.shadowOf(mainActivity).peekNextStairtedService(); assertEquals(MiService.class.getCanonicalName(),intent.getComponent().getClassName()); } 

    e então eu executo o teste e tudo está bem

    Robolectric 3.1+

    Crie uma instância do seu service

    Ligue onHandleIntent diretamente

      YourService svc = Robolectric.buildService(YourService.class).get(); Intent fakeIntent = new Intent(); fakeIntent.putExtra(YourService.SOME_EXTRA, "debug|fail"); svc.onHandleIntent(fakeIntent); assertThat(something.happened).isEqualTo(true); 

    Nota: use Robolectric.buildService (não .buildIntentService ) mesmo se você estiview testando um IntentService. .buildIntentService atualmente não está funcionando, tanto quanto eu posso dizer.

    Atualização 2017-05-17:

    O problema com buildIntentService será corrigido no futuro

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.