Java and Mockito Testing: ValidatableResponse and ValidatableResponseOptions

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật