Spring MockMvc 不期望内容
Spring MockMvc don't expect content
我正在尝试测试 returns 内容基于用户 spring 安全角色的 thymeleaf 模板。
我要检查某些内容是否不存在
@Autowired
private MockMvc mockMvc;
...
mockMvc.perform(get("/index"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("This content should be shown.")))
.andExpect(content().string(XXXXXXX("This content should not be shown")));
这可能吗?
一种解决方案是使用 hamcrests CoreMatchers.not(....) 方法:
@Test
@WithMockUser(roles = "USER")
public void loginWithRoleUserThenExpectUserSpecificContent() throws Exception {
mockMvc.perform(get("/index"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("This content is only shown to users.")))
.andExpect(content().string(doesNotContainString("This content is only shown to administrators.")));
}
private Matcher<String> doesNotContainString(String s) {
return CoreMatchers.not(containsString(s));
}
我正在尝试测试 returns 内容基于用户 spring 安全角色的 thymeleaf 模板。
我要检查某些内容是否不存在
@Autowired
private MockMvc mockMvc;
...
mockMvc.perform(get("/index"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("This content should be shown.")))
.andExpect(content().string(XXXXXXX("This content should not be shown")));
这可能吗?
一种解决方案是使用 hamcrests CoreMatchers.not(....) 方法:
@Test
@WithMockUser(roles = "USER")
public void loginWithRoleUserThenExpectUserSpecificContent() throws Exception {
mockMvc.perform(get("/index"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("This content is only shown to users.")))
.andExpect(content().string(doesNotContainString("This content is only shown to administrators.")));
}
private Matcher<String> doesNotContainString(String s) {
return CoreMatchers.not(containsString(s));
}