AndroidX Testi ile JUnit4 kuralları

AndroidX Testi, AndroidJUnitRunner'ı açın. JUnit kuralları daha fazla esneklik sağlar ve testlerde gereken ortak metin kodu. Örneğin, bir ekip üyesiyle aynı anda bir belirli bir aktiviteye dokunun.

EtkinlikSenaryosuKural

Bu kural, tek bir etkinliğin işlevsel testini sağlar. Kural başlatılır seçilen etkinlik her testten önce ve @Test ile ek açıklama olarak verilir. @Before ile başlayan herhangi bir yöntem. Kural, şu tarihten sonra etkinliği sonlandırır: test tamamlandı ve @After ile açıklama eklenen tüm yöntemler tamamlanır. Verilen etkinliği olması için, Search Console'da çalıştırılabilir bir ActivityScenarioRule.getScenario().onActivity()

Aşağıdaki kod snippet'i Test mantığınıza ActivityScenarioRule ekleyin:

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() { ... }
}

Hizmet Test Kuralı

Bu kural, ve öncesinde ya da sonrasında kapatmaktır. Hizmeti şununla başlatabilir veya bağlayabilirsiniz: diğer yöntemlerden biridir. Testten sonra otomatik olarak durur veya bağlantısı kesilir tamamlandı. @After ile ek açıklama eklenen tüm yöntemler tamamlandı.

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

Ek kaynaklar

Android testlerinde JUnit kurallarını kullanma hakkında daha fazla bilgi için şu sayfaya bakın: inceleyebilirsiniz.

Belgeler

Örnekler