gpt4 book ai didi

java - 出现错误 io.restassured.internal.ValidatableResponseImpl 无法转换为 io.restassured.response.Response

转载 作者:行者123 更新时间:2023-12-02 02:40:06 25 4
gpt4 key购买 nike

运行测试时出现以下错误。我正在尝试将 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);
}
}

关于java - 出现错误 io.restassured.internal.ValidatableResponseImpl 无法转换为 io.restassured.response.Response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45624726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com