我正在尝试测试可根据用户的春季安全角色返回内容的百里香模板。
我正在检查某些内容不存在
@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")));
这可能吗?
答案 0 :(得分:1)
一种解决方案是使用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));
}