I'm relatively new to Java testing and have a problem. I'm about to test a Rest client.I tested successfully all methods - put, get, delete. After I extended my delete method for a specific purpose I get an exception I cant solve:
`java.lang.ClassCastException: class io.restassured.response.ValidatableResponseOptions$MockitoMock$Alt0YnEv cannot be cast to class io.restassured.response.ValidatableResponse (io.restassured.response.ValidatableResponseOptions$MockitoMock$Alt0YnEv and io.restassured.response.ValidatableResponse are in unnamed module of loader 'app')`
Here is my code:
//the method to test
/** * Sends a delete request to given endpoint * * @param endpoint Rest endpoint * @param parameters valueName=value, valueName2=value2 ... (optional) * @return ValidatableResponse */public ValidatableResponse getJSONResourceDELETE2ArrayParams(String endpoint, String... parameters) { if(parameters.length > 1) { Response response = given() .cookie(this.cookie) .spec(getJsonRequestSpecification()) .params(parameters[0], parameters[1]) .when() .delete(this.injectionManager.inject(endpoint)); cookie = extractCookie(response); return response .then() .statusCode(200); } else { throw new IllegalArgumentException("This method needs at least two parameter"); }}
// the test
private void initRestAssured() { restClient.setLogging(RestClient.Logging.ALL); mockedRa.when(RestAssured::given).thenReturn(requestSpecification); when(requestSpecification.auth().preemptive().basic(anyString(), anyString()) .multiPart(file)).thenReturn(requestSpecification); when(requestSpecification.cookie(cookie)).thenReturn(requestSpecification); when(requestSpecification.spec(any())).thenReturn(requestSpecification); when(requestSpecification.queryParams(Mockito.any())).thenReturn(requestSpecification); when(requestSpecification.when()).thenReturn(requestSpecification); when(requestSpecification.get()).thenReturn(response); when(requestSpecification.body(Mockito.any(File.class))).thenReturn(requestSpecification); when(injectionManager.inject(anyString())).thenReturn("something injected"); when(requestSpecification.body(anyString())).thenReturn(requestSpecification); when(requestSpecification.post("something injected")).thenReturn(response); when(response.then()).thenReturn(validatableResponse); when(validatableResponse.statusCode(200)).thenReturn(validatableResponse); when(validatableResponseOptions.statusCode(200)).thenReturn(validatableResponse); when(validatableResponse.contentType(ContentType.JSON)).thenReturn(validatableResponse); when(validatableResponse.extract()).thenReturn(extractableResponse); when(extractableResponse.response()).thenReturn(response); when(response.as(String.class)).thenReturn("returned");}// TODO find issue with Validate Response@Testvoid testGetJSONResourceDELETE2ArrayParamsGivenEndPointAndParas_thenDelete() { // Given initRestAssured(); String param1 = "id"; String param2 = "name"; String[] parameters = new String[2]; parameters[0] = param1; parameters[1] = param2; // When restClient.getJSONResourceDELETE2ArrayParams(endpoint, parameters[0], parameters[1]); // Then verify(injectionManager, times(1)).inject(endpoint); verify(requestSpecification, times(1)).delete();}
I would be happy for any solution suggested :)