在Rest-sure中使用Json文件进行有效负载

Bha*_*hra 6 java rest junit json rest-assured

我有一个巨大的JSON文件作为休眠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");


}
Run Code Online (Sandbox Code Playgroud)

并得到错误:

java.lang.UnsupportedOperationException: Internal error: Can't encode /Users/bmishra/Code_Center/stash/experiments/src/main/resources/Search.json to JSON.
Run Code Online (Sandbox Code Playgroud)

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

经过充分研究后,它似乎无法运作.我已经解决了问题. https://github.com/jayway/rest-assured/issues/674

Bha*_*hra 7

向放心团队发布问题后。我已经解决了。我测试了该修复,问题现已解决。

来自放心的消息:

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

<repositories>
        <repository>
            <id>sonatype</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
            <snapshots />
        </repository>
</repositories>
Run Code Online (Sandbox Code Playgroud)

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


小智 5

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

public String generateStringFromResource(String path) throws IOException {

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

}
Run Code Online (Sandbox Code Playgroud)

所以在你的例子中:

@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"));
}
Run Code Online (Sandbox Code Playgroud)