简体   繁体   中英

Getting Error io.restassured.internal.ValidatableResponseImpl cannot be cast to io.restassured.response.Response

I am getting the following error upon running the test. I am trying to print the API response to a file, however the test is failing and throwing the error. The response of the call is in JSON and is in GZIP. Any thoughts and ideas are greatly appreciated.

Error : io.restassured.internal.ValidatableResponseImpl cannot be cast to io.restassured.response.Response

Here is my code :

package TestPackage;

import org.testng.Assert;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import io.restassured.response.Response;


public class ApiServerTest {

    String baseQAURL = "http://some:8282/getworkitemwithtime";


    @Test
    public void apiServerTest() throws FileNotFoundException{

        Response srvrResponse = (Response) given().
                                param("starttime", "2017-08-10T11:17:00").
                                param("endtime", "2017-08-10T11:17:01").
                                when().
                                get(baseQAURL).
                                then().
                                log().
                                all();

            System.out.println(srvrResponse.getStatusCode());
            Assert.assertEquals(srvrResponse.getStatusCode(), 200);

            srvrResponse.asString();
            PrintStream out = new PrintStream(new FileOutputStream("C:\\Test\\output.txt"));
            System.setOut(out);


    }

}

Comments are right - you need to extract your response to be used out of request context.

Actually you may optimize your code:

import io.restassured.config.LogConfig;
import org.testng.annotations.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

import static io.restassured.RestAssured.config;
import static io.restassured.RestAssured.given;

public class TestLogging {

    String baseQAURL = "https://httpbin.org/get";

    @Test
    public void apiServerTest() throws FileNotFoundException {
        config = config().logConfig(new LogConfig().defaultStream(new PrintStream(new File("C:\\Test\\output.txt"))));

        given().
                param("starttime", "2017-08-10T11:17:00").
                param("endtime", "2017-08-10T11:17:01").
                when().
                get(baseQAURL).
                then().
                log().
                all()
                .assertThat().statusCode(200);
    }
}

I got same error

.contentType(ContentType.JSON) .body(loginBody) .cookies(cookies) .post() .then().extract().response()

io.restassured.internal.http.ResponseParseException: status code: 200, reason phrase: OK

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM