JSON 路径标题不匹配
JSON path title doesn't match
我使用 rest-assured 编写了一个简单的测试,代码如下
@Test
public void exampleRestTest() {
RestAssured.baseURI = "https://jsonplaceholder.typicode.com/";
Response res = RestAssured.get("posts/1");
res.print();
res.then()
.body("id", equalTo(1))
.body("userId", equalTo(1))
.body("title",contains("sunt aut facere repellat provident occaecati excepturi optio reprehenderit"));
来自 res.print() 的 JSON 是:
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit suscipit recusandae consequuntur expedita et cum reprehenderit molestiae ut ut quas totam nostrum rerum est autem sunt rem eveniet architecto"
}
测试输出:
java.lang.AssertionError: 1 expectation failed.
JSON path title doesn't match.
Expected: iterable containing ["sunt aut facere repellat provident occaecati excepturi optio reprehenderit"]
Actual: sunt aut facere repellat provident occaecati excepturi optio reprehenderit
为什么?
我必须从 iterable containing?
中逃脱
您只想使用 'contains' 匹配器吗?
'contains' 匹配器检查预期的 Iterable 对象是否包含在实际的集合对象中。
您应该使用 'containsString' 匹配器,如下所示:
.body("title",containsString("sunt aut facere repellat provident occaecati excepturi optio reprehenderit"))
我使用 rest-assured 编写了一个简单的测试,代码如下
@Test
public void exampleRestTest() {
RestAssured.baseURI = "https://jsonplaceholder.typicode.com/";
Response res = RestAssured.get("posts/1");
res.print();
res.then()
.body("id", equalTo(1))
.body("userId", equalTo(1))
.body("title",contains("sunt aut facere repellat provident occaecati excepturi optio reprehenderit"));
来自 res.print() 的 JSON 是:
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit suscipit recusandae consequuntur expedita et cum reprehenderit molestiae ut ut quas totam nostrum rerum est autem sunt rem eveniet architecto"
}
测试输出:
java.lang.AssertionError: 1 expectation failed.
JSON path title doesn't match.
Expected: iterable containing ["sunt aut facere repellat provident occaecati excepturi optio reprehenderit"]
Actual: sunt aut facere repellat provident occaecati excepturi optio reprehenderit
为什么? 我必须从 iterable containing?
中逃脱您只想使用 'contains' 匹配器吗? 'contains' 匹配器检查预期的 Iterable 对象是否包含在实际的集合对象中。
您应该使用 'containsString' 匹配器,如下所示:
.body("title",containsString("sunt aut facere repellat provident occaecati excepturi optio reprehenderit"))