下面的代码显示了错误。类型推断失败。所需类型不匹配需要:找到了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>>