[英]Getting Error io.restassured.internal.ValidatableResponseImpl cannot be cast to io.restassured.response.Response
运行测试时出现以下错误。 我正在尝试将 API 响应打印到文件,但是测试失败并抛出错误。 调用的响应采用 JSON 格式,采用 GZIP 格式。 任何想法和想法都非常感谢。
错误:io.restassured.internal.ValidatableResponseImpl 无法转换为 io.restassured.response.Response
这是我的代码:
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);
}
}
评论是正确的-您需要提取您的响应以在请求上下文之外使用。
实际上,您可以优化代码:
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);
}
}
我有同样的错误
.contentType(ContentType.JSON) .body(loginBody) .cookies(cookies) .post() .then().extract().response()
io.restassured.internal.http.ResponseParseException:状态代码:200,原因短语:OK
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.