RestAssured测试hateoas

时间:2016-10-06 17:29:04

标签: rest-assured

我正在RestAssured中编写测试用例,以使用spring mvc测试其余的web服务。

休息响应是

{
  "links": [
{
  "rel": "self",
  "href": "http://www.localhost.com:8080/v1/communities?offset=0&limit=10"
},
{
  "rel": "next",
  "href": "http://www.localhost.com:8080/v1/communities?offset=10&limit=10"
}
  ],
  "content": [
{
.....

我的测试用例是

    when().
        get("/communities").
    then().
        root("links").
        body("href", new ResponseAwareMatcher() {
            public Matcher<? extends Object> matcher(ResponseBody response) {
                return equalTo(new String[] {"http://www.localhost.com:8080/v1/communities?offset=0&limit=10", "http://www.localhost.com:8080/v1/communities?offset=10&limit=10"});
            }
        });

测试用例失败,错误

java.lang.AssertionError: 1 expectation failed.
JSON path links.href doesn't match.
Expected: ["http://www.localhost.com:8080/v1/communities?offset=0&limit=10", "http://www.localhost.com:8080/v1/communities?offset=10&limit=10"]
Actual: [http://www.localhost.com:8080/v1/communities?offset=0&limit=10, http://www.localhost.com:8080/v1/communities?offset=10&limit=10]

我甚至尝试过

equalTo("[http://www.localhost.com:8080/v1/communities?offset=0&limit=10, http://www.localhost.com:8080/v1/communities?offset=10&limit=10");

将错误输出

java.lang.AssertionError: 1 expectation failed.
JSON path links.href doesn't match.
Expected: [http://www.localhost.com:8080/v1/communities?offset=0&limit=10, http://www.localhost.com:8080/v1/communities?offset=10&limit=10]]
Actual: [http://www.localhost.com:8080/v1/communities?offset=0&limit=10, http://www.localhost.com:8080/v1/communities?offset=10&limit=10]

我正在使用Rest Assured 3.0.1。感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

试试这个

assertEquals("http://www.localhost.com:8080/v1/communities?offset=0&limit=10", given().when().get("/communities").body().jsonPath().get("links[0].href"));