กฎ JUnit4 ที่ใช้ AndroidX Test

AndroidX Test มีชุดกฎ JUnit เพื่อใช้กับ AndroidJUnitRunner กฎ JUnit มีความยืดหยุ่นมากกว่าและลด ต้องใช้โค้ด Boilerplate ในการทดสอบ เช่น สามารถใช้แท็กเริ่มต้น กิจกรรมที่เจาะจง

กฎสถานการณ์กิจกรรม

กฎนี้ให้การทดสอบการทำงานของกิจกรรมเดียว กฎจะเริ่มต้น กิจกรรมที่เลือกก่อนการทดสอบแต่ละครั้ง ใส่คำอธิบายประกอบด้วย @Test และ เมธอดที่มีคำอธิบายประกอบ @Before กฎจะยุติกิจกรรมหลังจาก การทดสอบเสร็จสมบูรณ์และวิธีการทั้งหมดที่มีคำอธิบายประกอบด้วยการเสร็จสิ้น @After หากต้องการเข้าถึง กิจกรรมในตรรกะการทดสอบ ให้ระบุ Callback ที่สามารถเรียกใช้ได้ ActivityScenarioRule.getScenario().onActivity()

ข้อมูลโค้ดต่อไปนี้สาธิตวิธีการรวม ActivityScenarioRule ลงในตรรกะการทดสอบของคุณ:

KotlinJava

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

ServiceTestRule

กฎนี้มอบกลไกที่ใช้งานง่ายในการเปิดใช้บริการก่อน และปิดเครื่องทั้งก่อนและหลัง คุณจะเริ่มหรือเชื่อมโยงบริการได้ด้วย วิธีตัวช่วยอย่างหนึ่ง หยุดหรือยกเลิกการเชื่อมโยงโดยอัตโนมัติหลังการทดสอบ เสร็จสมบูรณ์ และเมธอดที่มีคำอธิบายประกอบ @After เสร็จเรียบร้อยแล้ว

KotlinJava

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

แหล่งข้อมูลเพิ่มเติม

ดูข้อมูลเพิ่มเติมเกี่ยวกับการใช้กฎ JUnit ในการทดสอบ Android ได้ที่ แหล่งข้อมูลต่อไปนี้

เอกสารประกอบ

ตัวอย่าง

  • BasicSample: การใช้ ActivityScenarioRule เป็นไปอย่างง่ายดาย