Testa il servizio

Se stai implementando una Service locale come componente dell'app, puoi creare test strumentati per verificarne il comportamento corretto.

AndroidX Test fornisce un'API per testare gli oggetti Service in isolamento. La classe ServiceTestRule è una regola JUnit 4 che avvia il servizio prima dell'esecuzione dei metodi di test delle unità e arresta il servizio dopo il completamento dei test. Per ulteriori informazioni sulle regole JUnit 4, consulta la documentazione di JUnit.

Configura l'ambiente di test

Prima di creare il test di integrazione per il servizio, assicurati di configurare il progetto per i test strumentati, come descritto in Configurare il progetto per il test AndroidX.

Creare un test di integrazione per i servizi

Il test di integrazione deve essere scritto come una classe di test JUnit 4. Per scoprire di più sulla creazione di classi di test JUnit 4 e sull'utilizzo dei metodi di asserzione JUnit 4, consulta Creare una classe di test strumentata.

Crea un'istanza ServiceTestRule nel test utilizzando l'annotazione @Rule.

Kotlin


@get:Rule
val serviceRule = ServiceTestRule()

Java


@Rule
public final ServiceTestRule serviceRule = new ServiceTestRule();

L'esempio seguente mostra come implementare un test di integrazione per un servizio. Il metodo di test testWithBoundService() verifica che l'app venga associata correttamente a un servizio locale e che l'interfaccia del servizio funzioni correttamente.

Kotlin


@Test
@Throws(TimeoutException::class)
fun testWithBoundService() {
  // Create the service Intent.
  val serviceIntent = Intent(
      ApplicationProvider.getApplicationContext<Context>(),
      LocalService::class.java
  ).apply {
    // Data can be passed to the service via the Intent.
    putExtra(SEED_KEY, 42L)
  }

  // Bind the service and grab a reference to the binder.
  val binder: IBinder = serviceRule.bindService(serviceIntent)

  // Get the reference to the service, or you can call
  // public methods on the binder directly.
  val service: LocalService = (binder as LocalService.LocalBinder).getService()

  // Verify that the service is working correctly.
  assertThat(service.getRandomInt(), `is`(any(Int::class.java)))
}

Java


@Test
public void testWithBoundService() throws TimeoutException {
  // Create the service Intent.
  Intent serviceIntent =
      new Intent(ApplicationProvider.getApplicationContext(),
        LocalService.class);

  // Data can be passed to the service via the Intent.
  serviceIntent.putExtra(LocalService.SEED_KEY, 42L);

  // Bind the service and grab a reference to the binder.
  IBinder binder = serviceRule.bindService(serviceIntent);

  // Get the reference to the service, or you can call
  // public methods on the binder directly.
  LocalService service =
      ((LocalService.LocalBinder) binder).getService();

  // Verify that the service is working correctly.
  assertThat(service.getRandomInt()).isAssignableTo(Integer.class);
}

Risorse aggiuntive

Per ulteriori informazioni su questo argomento, consulta le seguenti risorse aggiuntive.

Samples