使用 Rest-assured 中的 Json 文件作为负载

using a Json file in Rest-assured for payload

我有一个巨大的 JSON 文件要 POST 作为其余 api 调用的有效载荷用于测试目的。我试过类似的东西:

    public void RestTest() throws Exception {
    File file = new File("/Users/bmishra/Code_Center/stash/experiments/src/main/resources/Search.json");
    String content = null;

    given().body(file).with().contentType("application/json").then().expect().
            statusCode(200).
            body(equalTo("true")).when().post("http://devsearch");


}

并得到错误:

java.lang.UnsupportedOperationException: Internal error: Can't encode /Users/bmishra/Code_Center/stash/experiments/src/main/resources/Search.json to JSON.

我可以 运行 通过读取文件并将正文作为字符串传递,这是可行的,但我看到我可以直接传递文件对象,但这是行不通的。

经过充分研究,似乎它不起作用。我放心地打开了问题。 https://github.com/jayway/rest-assured/issues/674

我使用通用方法从 json 读取并将其作为字符串发送,即:

public String generateStringFromResource(String path) throws IOException {

    return new String(Files.readAllBytes(Paths.get(path)));

}

所以在你的例子中:

@Test
public void post() throws IOException {

   String jsonBody = generateStringFromResource("/Users/bmishra/Code_Center/stash/experiments/src/main/resources/Search.json")

    given().
            contentType("application/json").
            body(jsonBody).
    when().
            post("http://dev/search").
    then().
            statusCode(200).
            body(containsString("true"));
}

与放心的团队发布问题后。我有办法解决。我测试了修复程序,现在问题已解决。

放心留言:

它现在应该已修复,所以我现在部署了一个应该可以解决此问题的新快照。请在添加以下 Maven 存储库后尝试版本 2.9.1-SNAPSHOT:

<repositories>
        <repository>
            <id>sonatype</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
            <snapshots />
        </repository>
</repositories>

更多信息:https://github.com/jayway/rest-assured/issues/674#issuecomment-210455811