Тестирование Java и Mockito: ValidatableResponse и ValidatableResponseOptionsJAVA

Программисты JAVA общаются здесь
Anonymous
Тестирование Java и Mockito: ValidatableResponse и ValidatableResponseOptions

Сообщение Anonymous »

Я относительно новичок в тестировании Java, и у меня возникла проблема. Я собираюсь протестировать клиент Rest.
Я успешно протестировал все методы — поместить, получить, удалить. После того как я расширил свой метод удаления для определенной цели, я получил исключение, которое не могу устранить:

Код: Выделить всё

`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')`
Вот мой код:
//метод для тестирования

Код: Выделить всё

/**
* 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");
}
}
// тест

Код: Выделить всё

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();
}
Буду рад любому предложенному решению :)

Подробнее здесь: https://stackoverflow.com/questions/786 ... nseoptions

Вернуться в «JAVA»