简体   繁体   中英

Type inference failed. Expected type mismatch

The code below shows the error. Type inference failed. Expected type mismatch required : Response<BaseResponse<Any>>! found : Response<BaseResponse<RetriveUserInfoResponse>!>!

`when`( mockIOnboardingService.validateCustomerIdentity(customerType.toLowerCase(), ValidateCustomerRequest(customerId, documentType, "243546", tyc)))
.thenReturn(Response.success(BaseResponse(payload = RetriveUserInfoResponse("+5689765432")))) //--> Here the error

This is the validateCustomerIdentity method

@POST(ApiConstants.bffOnboardingPath + ApiConstants.pathRetriveUserInfo)
    suspend fun validateCustomerIdentity(
        @Header(ApiConstants.headerXflowService) customerType : String,
        @Body body: ValidateCustomerRequest
    ): Response<BaseResponse<Any>>

As you can see it returns BaseResponse<Any> . Why Android Studio is showing me BaseResponse<RetriveUserInfoResponse>! as an error

This is the RetrieveUserInfoResponse data class

data class RetriveUserInfoResponse(
    @SerializedName("phone")
    val phone: String
)

This problem is that Response.success(BaseResponse(payload = RetriveUserInfoResponse("+5689765432"))) produces a Response<BaseResponse<RetriveUserInfoResponse>> , which is not the same type (or a subtype) of Response<BaseResponse<Any>> .

You can fix it by casting RetriveUserInfoResponse to Any :

Response.success(BaseResponse(payload = RetriveUserInfoResponse("+5689765432") as Any))

Or alternatively by changing the return type of validateCustomerIdentity() to Response<out BaseResponse<out Any>> , which works because Response<BaseResponse<RetriveUserInfoResponse>> is a subclass of 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>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM