2020年6月16日火曜日

WorkManager の CoroutineWorker, Worker をテストする

こういう CoroutineWorker があるとします。
  1. class SendHelloWorker(  
  2.     private val context: Context,  
  3.     params: WorkerParameters  
  4. ) : CoroutineWorker(context, params) {  
  5.   
  6.     override suspend fun doWork(): Result {  
  7.         val name = inputData.getString("name") ?: return Result.failure()  
  8.   
  9.         val api = (context.applicationContext as MyApplication)  
  10.             .appComponent  
  11.             .api()  
  12.   
  13.         return try {  
  14.             api.sendHello("Hello $name")  
  15.             Result.success()  
  16.         } catch (e: Exception) {  
  17.             Result.failure()  
  18.         }  
  19.     }  
  20. }  
まず CoroutineWorker や Worker をテストするには worker-testing artifact を使います。
  1. testImplementation "androidx.work:work-testing:$work_version"  
CoroutineWorker のテストには TestListenableWorkerBuilder を使います。
TestListenableWorkerBuilder は version 2.1.0 から追加されています。

TestListenableWorkerBuilder の build() で Worker のインスタンスを取得し、doWork() を呼んで結果をチェックします。
  1. class SendHelloWorkerTest {  
  2.   
  3.     private lateinit var api: MyApi  
  4.     private lateinit var context: Context  
  5.   
  6.     @Before  
  7.     fun setup() {  
  8.         api = mock()  
  9.   
  10.         val appComponent: AppComponent = mock()  
  11.         whenever(appComponent.api()).thenReturn(api)  
  12.   
  13.         val application = MyApplication()  
  14.         application.setAppComponent(appComponent)  
  15.   
  16.         context = mock()  
  17.         whenever(context.applicationContext).thenReturn(application)  
  18.     }  
  19.   
  20.     @Test  
  21.     fun doWork() {  
  22.         val worker = TestListenableWorkerBuilder<SendHelloWorker>(  
  23.             context,  
  24.             inputData = Data.Builder()  
  25.                 .putString("name""Android")  
  26.                 .build()  
  27.         )  
  28.             .build()  
  29.   
  30.         runBlocking {  
  31.             val result = worker.doWork()  
  32.   
  33.             assertThat(result).isEqualTo(ListenableWorker.Result.success())  
  34.             verify(api).sendHello("Hello Android")  
  35.         }  
  36.     }  
  37.   
  38.     @Test  
  39.     fun doWork_fail() {  
  40.         val worker = TestListenableWorkerBuilder<SendHelloWorker>(  
  41.             context,  
  42.             inputData = Data.Builder()  
  43.                 .putString("name""Android")  
  44.                 .build()  
  45.         )  
  46.             .build()  
  47.   
  48.         whenever(api.sendHello("Hello Android")).thenThrow(RuntimeException())  
  49.   
  50.         runBlocking {  
  51.             val result = worker.doWork()  
  52.   
  53.             assertThat(result).isEqualTo(ListenableWorker.Result.failure())  
  54.             verify(api).sendHello("Hello Android")  
  55.         }  
  56.     }  
  57. }  


CoroutineWorker ではなく Worker をテストするときは TestListenableWorkerBuilder ではなく TestWorkerBuilder を使います。



参考 : https://developer.android.com/topic/libraries/architecture/workmanager/how-to/testing-worker-impl



0 件のコメント:

コメントを投稿