MockMvc执行,数组响应断言失败



我正在为Spring Boot实现集成测试。我的一个端点会返回所有用户,甚至是已删除的用户。此处deleted表示已删除的布尔属性在AppUser实体上设置为true(~soft-delete(。端点工作得很好,我已经用Postman进行了手动测试(您可以在下面的响应中看到(。代码:

@Test
void testGetAllAppUsers_whenAuthorizedOnlyActiveFalse_gotDeletedUser() throws Exception {
String token = jwtProvider.generateToken(TestUtils.ACCOUNT_MANAGER_USERNAME);
String url = "/api/account/user";
mvc.perform(get(url)
.header("Authorization", token)
.param("only-active", "false"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[*].deleted").value(Matchers.arrayContaining(true)));
}

响应:

...
Body = [{"id":7,"username":"ablabadmin@account.com","fullName":"Alpha Beta Lab Admin","deleted":false,"labsAsUser":[],"labsAsAdmin":[],"roles":[],"managedLabs":[]},{"id":11,"username":"ablabdeleteduser@account.com","fullName":"Alpha Beta Lab Deleted User","deleted":true,"labsAsUser":[],"labsAsAdmin":[],"roles":[],"managedLabs":[]},{"id":4,"username":"ablabman@account.com","fullName":"Alpha Beta Lab Manager","deleted":false,"labsAsUser":[],"labsAsAdmin":[],"roles":[],"managedLabs":[{"id":1,"key":"alab","name":"Alpha Lab","deleted":false,"createdAt":"2021-06-09","updatedAt":null,"labManagerUsernames":["alabman@account.com","ablabman@account.com"]},{"id":2,"key":"blab","name":"Beta Lab","deleted":false,"createdAt":"2021-06-09","updatedAt":null,"labManagerUsernames":["blabman@account.com","ablabman@account.com"]}]},{"id":10,"username":"ablabuser@account.com","fullName":"Alpha Beta Lab User","deleted":false,"labsAsUser":[],"labsAsAdmin":[],"roles":[],"managedLabs":[]},{"id":5,"username":"alabadmin@account.com","fullName":"Alpha Lab Admin","deleted":false,"labsAsUser":[],"labsAsAdmin":[],"roles":[],"managedLabs":[]},{"id":2,"username":"alabman@account.com","fullName":"Alpha Lab Manager","deleted":false,"labsAsUser":[],"labsAsAdmin":[],"roles":[],"managedLabs":[{"id":1,"key":"alab","name":"Alpha Lab","deleted":false,"createdAt":"2021-06-09","updatedAt":null,"labManagerUsernames":["alabman@account.com","ablabman@account.com"]}]},{"id":8,"username":"alabuser@account.com","fullName":"Alpha Lab User","deleted":false,"labsAsUser":[],"labsAsAdmin":[],"roles":[],"managedLabs":[]},{"id":1,"username":"aman@account.com","fullName":"Account Manager","deleted":false,"labsAsUser":[],"labsAsAdmin":[],"roles":[{"id":1,"role":"ACCOUNT_MANAGER"}],"managedLabs":[]},{"id":6,"username":"blabadmin@account.com","fullName":"Beta Lab Admin","deleted":false,"labsAsUser":[],"labsAsAdmin":[],"roles":[],"managedLabs":[]},{"id":3,"username":"blabman@account.com","fullName":"Beta Lab Manager","deleted":false,"labsAsUser":[],"labsAsAdmin":[],"roles":[],"managedLabs":[{"id":2,"key":"blab","name":"Beta Lab","deleted":false,"createdAt":"2021-06-09","updatedAt":null,"labManagerUsernames":["blabman@account.com","ablabman@account.com"]}]},{"id":9,"username":"blabuser@account.com","fullName":"Beta Lab User","deleted":false,"labsAsUser":[],"labsAsAdmin":[],"roles":[],"managedLabs":[]}]
Forwarded URL = null
Redirected URL = null
Cookies = []

java.lang.AssertionError: JSON path "$[*].deleted"
Expected: [<true>] 
but: was a net.minidev.json.JSONArray (<[false,true,false,false,false,false,false,false,false,false,false]>)
Expected :[<true>] 
Actual   :a net.minidev.json.JSONArray (<[false,true,false,false,false,false,false,false,false,false,false]>)
...

我尝试过许多其他参数:"true"false我尝试过contains方法,也尝试过使用现有用户名的"$[*].username"regex,但每次我都会得到AssertionError。你能帮帮我吗?我肯定错过了一些东西。此外,任何建议都是受欢迎的,因为我在Spring集成测试方面相对较新。提前感谢!

arrayContainingInAnyOrder(true,false)应该在这种情况下工作。

但是,即使测试用例使用arrayContainingInAnyOrder(true,false),它也不会有任何值。

正如您所看到的,[*].deleted为响应中存在的所有对象返回值deleted。测试必须验证特定值。例如,如果您期望第一个用户应该具有true,那么断言$.[?(@.id ==1)].deleted为true。

相关内容

  • 没有找到相关文章

最新更新