Il test AndroidX include un insieme di regole JUnit da utilizzare con AndroidJUnitRunner Le regole JUnit offrono maggiore flessibilità e riducono il codice boilerplate richiesto nei test. Ad esempio, possono essere usate per avviare per un'attività specifica.
Regola dello scenario di attività
Questa regola fornisce i test funzionali di una singola attività. La regola viene avviata
l'attività scelta prima di ogni test annotata con @Test
, nonché prima
qualsiasi metodo annotato con @Before
. La regola termina l'attività dopo
test completati e tutti i metodi annotati con @After
di completamento. Per accedere ai dati forniti
attività nella logica di test, fornisci un callback eseguibile
ActivityScenarioRule.getScenario().onActivity()
.
Il seguente snippet di codice mostra come incorporare
ActivityScenarioRule
nella logica di test:
@RunWith(AndroidJUnit4::class.java)
@LargeTest
class MyClassTest {
@get:Rule
val activityRule = ActivityScenarioRule(MyClass::class.java)
@Test fun myClassMethod_ReturnsTrue() {
activityRule.scenario.onActivity { … } // Optionally, access the activity.
}
}
public class MyClassTest {
@Rule
public ActivityScenarioRule<MyClass> activityRule =
new ActivityScenarioRule(MyClass.class);
@Test
public void myClassMethod_ReturnsTrue() { ... }
}
RegolaServiceTest
Questa regola fornisce un meccanismo semplificato per avviare il servizio prima che
i test e arrestarlo prima e dopo. Puoi avviare o associare il servizio con
uno dei metodi helper. Si interrompe o si slega automaticamente dopo il test
vengono completati e tutti i metodi annotati con @After
sono terminati.
@RunWith(AndroidJUnit4::class.java)
@MediumTest
class MyServiceTest {
@get:Rule
val serviceRule = ServiceTestRule()
@Test fun testWithStartedService() {
serviceRule.startService(
Intent(ApplicationProvider.getApplicationContext<Context>(),
MyService::class.java))
// Add your test code here.
}
@Test fun testWithBoundService() {
val binder = serviceRule.bindService(
Intent(ApplicationProvider.getApplicationContext(),
MyService::class.java))
val service = (binder as MyService.LocalBinder).service
assertThat(service.doSomethingToReturnTrue()).isTrue()
}
}
@RunWith(AndroidJUnit4.class)
@MediumTest
public class MyServiceTest {
@Rule
public final ServiceTestRule serviceRule = new ServiceTestRule();
@Test
public void testWithStartedService() {
serviceRule.startService(
new Intent(ApplicationProvider.getApplicationContext(),
MyService.class));
// Add your test code here.
}
@Test
public void testWithBoundService() {
IBinder binder = serviceRule.bindService(
new Intent(ApplicationProvider.getApplicationContext(),
MyService.class));
MyService service = ((MyService.LocalBinder) binder).getService();
assertThat(service.doSomethingToReturnTrue()).isTrue();
}
}
Risorse aggiuntive
Per ulteriori informazioni sull'uso delle regole JUnit nei test Android, consulta le seguenti risorse.
Documentazione
- Guida Testa i frammenti per testare i frammenti in modo isolato.
- Testare il layout di Compose per testare le UI create con Compose.
Campioni
- BasicSample: utilizzo semplice di
ActivityScenarioRule
.