繁体   English   中英

在 Rest-assured 中使用 Json 文件作为有效负载

[英]using a Json file in Rest-assured for payload

我有一个巨大的 JSON 文件作为用于测试目的的 rest api 调用的有效负载进行 POST。 我试过类似的东西:

    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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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