0

Ninja Framework ( https://www.ninjaframework.org/documentation/testing_your_application/advanced.html )との統合テストを実行しようとしています。このサービスには、レトロフィットを使用してサードパーティ API と対話する apiClient インスタンスがあります。

class Service
@Inject
constructor(
        private val apiClient: ApiClient
)

apiClient.call のレスポンスをモックしたい。Mock で注釈を付けた apiClent を設定するか、Service(apiClient) でサービスを初期化しようとしましたが、実際の API と対話し、Timeout 応答を返します。

@RunWith(NinjaRunner::class)
class IntegrationTest {
    var apiClient: ApiClient = mock()

    @Inject
    var service: Service= mock()

    @Test
    fun `test something`() {
        whenever(apiClient.call()).thenReturn(
                RestResponse(status = RestResponse.Status.SUCCESS, message = "success")
        )

        val result = service.update()
    }
}
4

1 に答える 1

0

統合テストは、さまざまなモジュールがグループとして組み合わされたときに正常に動作するかどうかをチェックすることを意味します。

apiClient モックを使用して Service をテストしているため、おそらくここで必要なのは単体​​テストです。

実際にテストしているクラスをモックしたくないので、ここでの 1 つの方法は、Service をモック オブジェクトで初期化し、別の方法では @Mock アノテーションを使用して実行時にモックを作成します。詳細はこちらhttps://www.vogella.com/tutorials/Mockito/article.html

@RunWith(NinjaRunner::class)
class IntegrationTest {
    var apiClient: ApiClient = mock()

    var service: Service = Service(apiClient)

    @Test
    fun `test something`() {
        whenever(apiClient.call()).thenReturn(
                RestResponse(status = RestResponse.Status.SUCCESS, message = "success")
        )

        val result = service.update()
    }
}
于 2020-08-26T11:54:05.443 に答える