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:
<code>`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')`
</code>
<code>`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')`
</code>
`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
<code>/**
* 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");
}
}
</code>
<code>/**
* 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");
}
}
</code>
/**
* 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
<code>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
@Test
void 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();
}
</code>
<code>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
@Test
void 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();
}
</code>
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
@Test
void 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 🙂
New contributor
user25924792 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.