Reputation: 87
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);
}
}
Upvotes: 0
Views: 8998
Reputation: 2609
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);
}
}
Upvotes: 3