2016-10-06 93 views
0

我正在编写测试用例在RestAssured中使用spring mvc测试其他webservices。RestAssured测试仇恨

休息响应

{ 
    "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] 

我用的放心3.0.1。感谢您的帮助提前。

回答

0

试试这个

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