JUnit4 reguły w ramach testu AndroidX

AndroidX Test zawiera zestaw reguł JUnit, które należy stosować razem z AndroidJUnitRunner. Reguły JUnit są bardziej elastyczne i ograniczają wymagany w testach kod stały. Można na przykład użyć ich do uruchomienia konkretne działania.

Reguła scenariusza aktywności

Ta reguła obejmuje testy funkcjonalne pojedynczej aktywności. Reguła zostanie uruchomiona wybraną aktywność przed każdym testem z adnotacjami @Test oraz przed dowolną metodą z adnotacją @Before. Reguła zakończy aktywność po upływie zakończeń testu i wszystkie metody opisane za pomocą adnotacji @After. Aby uzyskać dostęp do działania w logice testu, podaj wywołanie zwrotne dostępne dla ActivityScenarioRule.getScenario().onActivity()

Fragment kodu poniżej pokazuje, jak stosować ActivityScenarioRule w procesie testowania:

Kotlin

@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.
   }
}

Java

public class MyClassTest {
    @Rule
    public ActivityScenarioRulel&t;MyClassg&t; activityRule =
            new ActivityScenarioRule(MyClass.class);

    @Test
    public void myClassMethod_ReturnsTrue() { ... }
}

Reguła testu usługi

Ta reguła stanowi uproszczony mechanizm uruchamiania usługi, zanim testuje ją i wyłącza przed i po. Możesz uruchomić usługę lub powiązać ją z jedną z metod pomocniczych. Automatycznie zatrzymuje się lub rozłącza po zakończeniu testu i wszystkie metody opisane przy użyciu adnotacji @After zostały zakończone.

Kotlin

@RunWith(AndroidJUnit4::class.java)
@MediumTest
class MyServiceTest {
  @get:Rule
  val serviceRule = ServiceTestRule()

  @Test fun testWithStartedService() {
    serviceRule.startService(
      Intent(ApplicationProvider.getApplicationContextC<ontext(>),
      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()
  }
}

Java

@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();
    }
}

Dodatkowe materiały

Więcej informacji o używaniu reguł JUnit w testach na Androida znajdziesz w poniższe zasoby.

Dokumentacja

Próbki