Spring JsonPath 包含任何顺序异常
Spring JsonPath contains in any order exception
我正在用 spring 编写一些集成测试,想检查响应中的 json 是否包含所有必需的数据。见代码:
@Test
public void getAll() throws Exception {
String url = "/permissions/all";
int size = 4;
try {
Set<String> permissions = RandomGenerator.randomStrings(size);
initDb(permissions);
mvc.perform(get(url).with(Users.PERMISSIONS_READ))
.andExpect(jsonPath("$[?(@.name)].name", hasSize(size))) //ok
.andExpect(jsonPath("$[?(@.name)].name").value(containsInAnyOrder(permissions))); //exception
} finally {
dropDb();
}
}
但我遇到了一个例外:
java.lang.AssertionError: JSON path "$[?(@.name)].name"
Expected: iterable over [<[b0ce9e8a-8b62-41f4-91b9-d9cf6ff96675, 7ebe3a4f-7864-4ea5-92b3-a5ad6f44bf42, 7df2fa88-b22f-4d33-91b2-f8b00813522f, 17b61119-c48c-4dff-ac9c-047eb3efcc43]>] in any order
but: Not matched: "7df2fa88-b22f-4d33-91b2-f8b00813522f"
以及来自此响应的数据:
[{
"id": 1,
"name": "7df2fa88-b22f-4d33-91b2-f8b00813522f"
}, {
"id": 2,
"name": "b0ce9e8a-8b62-41f4-91b9-d9cf6ff96675"
}, {
"id": 3,
"name": "7ebe3a4f-7864-4ea5-92b3-a5ad6f44bf42"
}, {
"id": 4,
"name": "17b61119-c48c-4dff-ac9c-047eb3efcc43"
}]
我知道,选择器 $[?(@.name)].name
工作正常,returns 结果如下:
[
"7df2fa88-b22f-4d33-91b2-f8b00813522f",
"b0ce9e8a-8b62-41f4-91b9-d9cf6ff96675",
"7ebe3a4f-7864-4ea5-92b3-a5ad6f44bf42",
"17b61119-c48c-4dff-ac9c-047eb3efcc43"
]
permissions
set 也是正确的,包含上面示例中的 4 个字符串。
有人可以告诉我我做错了什么吗?
这会起作用:
Set<String> permissions = RandomGenerator.randomStrings(size);
initDb(permissions);
mvc.perform(get(url).with(Users.PERMISSIONS_READ))
.andExpect(jsonPath("$[?(@.name)].name", hasSize(size))) //ok
.andExpect(jsonPath("$[?(@.name)].name").value(containsInAnyOrder(permissions.toArray(new String[permissions.size()]))));
或重申:
String[] permissions = RandomGenerator.randomStrings(size);
initDb(permissions);
mvc.perform(get(url).with(Users.PERMISSIONS_READ))
.andExpect(jsonPath("$[?(@.name)].name", hasSize(size))) //ok
.andExpect(jsonPath("$[?(@.name)].name").value(containsInAnyOrder(permissions)));
这是containsInAnyOrder
的签名:
public static <T> Matcher<Iterable<? extends T>> containsInAnyOrder(T... items)
因此,它需要一个与您声明反对的 相同 类型的可变参数。在您的情况下,您针对 String
类型进行断言,但您向 containsInAnyOrder
提供 Set<String>
,因此 String 和 Set 之间的匹配失败。
我正在用 spring 编写一些集成测试,想检查响应中的 json 是否包含所有必需的数据。见代码:
@Test
public void getAll() throws Exception {
String url = "/permissions/all";
int size = 4;
try {
Set<String> permissions = RandomGenerator.randomStrings(size);
initDb(permissions);
mvc.perform(get(url).with(Users.PERMISSIONS_READ))
.andExpect(jsonPath("$[?(@.name)].name", hasSize(size))) //ok
.andExpect(jsonPath("$[?(@.name)].name").value(containsInAnyOrder(permissions))); //exception
} finally {
dropDb();
}
}
但我遇到了一个例外:
java.lang.AssertionError: JSON path "$[?(@.name)].name"
Expected: iterable over [<[b0ce9e8a-8b62-41f4-91b9-d9cf6ff96675, 7ebe3a4f-7864-4ea5-92b3-a5ad6f44bf42, 7df2fa88-b22f-4d33-91b2-f8b00813522f, 17b61119-c48c-4dff-ac9c-047eb3efcc43]>] in any order
but: Not matched: "7df2fa88-b22f-4d33-91b2-f8b00813522f"
以及来自此响应的数据:
[{
"id": 1,
"name": "7df2fa88-b22f-4d33-91b2-f8b00813522f"
}, {
"id": 2,
"name": "b0ce9e8a-8b62-41f4-91b9-d9cf6ff96675"
}, {
"id": 3,
"name": "7ebe3a4f-7864-4ea5-92b3-a5ad6f44bf42"
}, {
"id": 4,
"name": "17b61119-c48c-4dff-ac9c-047eb3efcc43"
}]
我知道,选择器 $[?(@.name)].name
工作正常,returns 结果如下:
[
"7df2fa88-b22f-4d33-91b2-f8b00813522f",
"b0ce9e8a-8b62-41f4-91b9-d9cf6ff96675",
"7ebe3a4f-7864-4ea5-92b3-a5ad6f44bf42",
"17b61119-c48c-4dff-ac9c-047eb3efcc43"
]
permissions
set 也是正确的,包含上面示例中的 4 个字符串。
有人可以告诉我我做错了什么吗?
这会起作用:
Set<String> permissions = RandomGenerator.randomStrings(size);
initDb(permissions);
mvc.perform(get(url).with(Users.PERMISSIONS_READ))
.andExpect(jsonPath("$[?(@.name)].name", hasSize(size))) //ok
.andExpect(jsonPath("$[?(@.name)].name").value(containsInAnyOrder(permissions.toArray(new String[permissions.size()]))));
或重申:
String[] permissions = RandomGenerator.randomStrings(size);
initDb(permissions);
mvc.perform(get(url).with(Users.PERMISSIONS_READ))
.andExpect(jsonPath("$[?(@.name)].name", hasSize(size))) //ok
.andExpect(jsonPath("$[?(@.name)].name").value(containsInAnyOrder(permissions)));
这是containsInAnyOrder
的签名:
public static <T> Matcher<Iterable<? extends T>> containsInAnyOrder(T... items)
因此,它需要一个与您声明反对的 相同 类型的可变参数。在您的情况下,您针对 String
类型进行断言,但您向 containsInAnyOrder
提供 Set<String>
,因此 String 和 Set 之间的匹配失败。