'Mock Retrofit error okhttp3.ResponseBody$1 cannot be cast

I'm trying to test a snippet of my code that uses retrofit, so I've created an interface to "mock" the return, it works in parts, it can invoke the onresponse of enqueue, however it can not convert Response <AuthResponse> response, follows the error:

java.lang.ClassCastException: okhttp3.ResponseBody$1 cannot be cast to br.com.safety.safetyrecognitionsdk.data.network.auth.AuthResponse

interface to mock

public interface AuthRepositoryBoundary {

    Call<AuthResponse> auth(String appKey, String projectId);

}

Class mocked

public class AuthSuccessTest implements AuthRepositoryBoundary {

    @Override
    public Call<AuthResponse> auth(String appKey, String projectId) {

        ResponseBody body = ResponseBody.create(
                MediaType.parse("application/json"),
                MockAuth.AUTH_SUCCESS
        );

        Response aResponse = Response.success(body, new okhttp3.Response.Builder() //
                .code(201)
                .message("OK")
                .body(body)
                .protocol(Protocol.HTTP_1_1)
                .request(new Request.Builder().url("http://localhost/").build())
                .build());

        return Calls.response(aResponse);
    }

}

implementation

@Override
public void auth(String appKey, String projectID) {
     this.authRepository.auth(appKey, projectID).enqueue(new Callback<AuthResponse>() {
          @Override
          public void onResponse(Call<AuthResponse> call, Response<AuthResponse> response) {
              switch (response.code()) {
                  case 201:
                      authListener.onSuccess(response.body());
                      break;
                  case 401:
                      authListener.onUnauthorized("Unauthorized");
                      break;
                  default:
                      authListener.onError("Server error");
                      break;
              }
         }

          @Override
          public void onFailure(Call<AuthResponse> call, Throwable t) {
              authListener.onError(t.getMessage());
            }
        });
    }

The test:

@Test
public void when_success_authentication_should_be_invoke_on_success() {
    this.authListenerMock = mock(AuthListener.class);
    this.authRepositoryMock = mock(AuthRepositoryBoundary.class);

    this.authSuccessTest = new AuthSuccessTest();

    this.safetyRecognition = new SafetyRecognition()
                .setCredentials("project_id", "key_id")
                .auth(new Authentication(this.authListenerMock, this.authSuccessTest));

    verify(this.authListenerMock).onSuccess(ArgumentMatchers.any(AuthResponse.class));
}


Solution 1:[1]

should it be

public interface AuthRepositoryBoundary {
Call<Response<AuthResponse>> auth(String appKey, String projectId);
}

and then:

public class AuthSuccessTest implements AuthRepositoryBoundary {

@Override
public Call<Response<AuthResponse>> auth(String appKey, String projectId) {

    ResponseBody body = ResponseBody.create(
            MediaType.parse("application/json"),
            MockAuth.AUTH_SUCCESS
    );

    Response aResponse = Response.success(body, new okhttp3.Response.Builder() //
            .code(201)
            .message("OK")
            .body(body)
            .protocol(Protocol.HTTP_1_1)
            .request(new Request.Builder().url("http://localhost/").build())
            .build());

    return Calls.response(aResponse);
}}

?

Solution 2:[2]

This solution

 @Override
 public Call<AuthResponse> auth(String appKey, String projectId) {
    ResponseBody body = ResponseBody.create(MediaType.parse("application/json"), JsonMockAuth.AUTH_SUCCESS);

    AuthResponse authResponse = new AuthResponse();
    authResponse.setxToken("tokinho");

    Response<AuthResponse> aResponse = Response.success(authResponse, new okhttp3.Response.Builder()
            .code(201)
            .message("OK")
            .protocol(Protocol.HTTP_1_1)
            .request(new Reqest.Builder().url("http://localhost/").build())
            .build());
    return Calls.response(aResponse);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 edhair
Solution 2 Jose Vieira Neto