翻新+ Mockito,设置为回拨电话

时间:2018-07-18 15:09:36

标签: android kotlin mockito retrofit

我的演示者功能:

 fun validateCompmanCode(code: String, uuid: String?) {
  view?.showLoading(resources.getString(R.string.validating_compman_code))
            async(UI) {
                bg {
                    try {
                        validateCompManCodeAndFetchCompanies(code, uuid)
                    } catch (e:Exception) {
                        handleException(e)
                    }

                }.await()
            }

    }

validateCompManCodeAndFetchCompanies进行了以下改造:

val response = apiBag.apiMainSlow.check_code(JWT.jwt_encrypted(request)).execute()

函数check_code返回Call

@POST("v2/check-code")
    fun check_code(@Body body: JwtDataRequest): Call<SyncCompManAndDeviceResponse>

并尝试为此创建一个单元测试,该测试如下所示:

val mockedResponse = gson.fromJson(COMPMAN_IMEIRECORD_RESPONSE_SUCCESS, SyncCompManAndDeviceResponse::class.java)


server.enqueue(MockResponse()
.setResponseCode(200)
.setBody(gson.toJson(mockedResponse)))
...

然后我在测试中调用该函数:

  doReturn(mockedResponse).`when`(api).check_code(mockedRequest).execute()
        presenter.validateCompmanCode("A", null)

,错误是:

org.mockito.exceptions.misusing.WrongTypeOfReturnValue: 
SyncCompManAndDeviceResponse cannot be returned by check_code()
check_code() should return Call

我当然知道这个错误,但是我不知道如何使doReturn返回Call<MyCustomObject>

0 个答案:

没有答案