下面的代码显示该错误。类型推断失败。所需的预期类型不匹配:Response<BaseResponse<Any>>!
找到:Response<BaseResponse<RetriveUserInfoResponse>!>!
`when`( mockIOnboardingService.validateCustomerIdentity(customerType.toLowerCase(), ValidateCustomerRequest(customerId, documentType, "243546", tyc)))
.thenReturn(Response.success(BaseResponse(payload = RetriveUserInfoResponse("+5689765432")))) //--> Here the error
这是validateCustomerIdentity方法
@POST(ApiConstants.bffOnboardingPath + ApiConstants.pathRetriveUserInfo)
suspend fun validateCustomerIdentity(
@Header(ApiConstants.headerXflowService) customerType : String,
@Body body: ValidateCustomerRequest
): Response<BaseResponse<Any>>
如您所见,它返回BaseResponse<Any>
。为什么Android Studio向我显示BaseResponse<RetriveUserInfoResponse>!
错误
这是RetrieveUserInfoResponse数据类
data class RetriveUserInfoResponse(
@SerializedName("phone")
val phone: String
)
这个问题是Response.success(BaseResponse(payload = RetriveUserInfoResponse("+5689765432")))
产生一个Response<BaseResponse<RetriveUserInfoResponse>>
与的类型(或子类型)不同的Response<BaseResponse<Any>>
。
您可以通过铸造修复RetriveUserInfoResponse
到Any
:
Response.success(BaseResponse(payload = RetriveUserInfoResponse("+5689765432") as Any))
或者通过将返回类型更改validateCustomerIdentity()
为Response<out BaseResponse<out Any>>
,这是有效的,因为它Response<BaseResponse<RetriveUserInfoResponse>>
是的子类Response<out BaseResponse<out Any>>
:
@POST(ApiConstants.bffOnboardingPath + ApiConstants.pathRetriveUserInfo)
suspend fun validateCustomerIdentity(
@Header(ApiConstants.headerXflowService) customerType : String,
@Body body: ValidateCustomerRequest
): Response<out BaseResponse<out Any>>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句