Spring mvc Spring MockMvc不期望内容
我正在尝试测试一个thymeleaf模板,该模板根据用户的spring安全角色返回内容 我希望检查一些内容是否不存在Spring mvc Spring MockMvc不期望内容,spring-mvc,spring-security,spring-test,Spring Mvc,Spring Security,Spring Test,我正在尝试测试一个thymeleaf模板,该模板根据用户的spring安全角色返回内容 我希望检查一些内容是否不存在 @Autowired private MockMvc mockMvc; 这可能吗?一种解决方案是使用hamcrests CoreMatchers。而不是。。。。方法: @Test @WithMockUser(roles = "USER") public void loginWithRoleUserThenExpectUserSpecificContent() throws E
@Autowired
private MockMvc mockMvc;
这可能吗?一种解决方案是使用hamcrests CoreMatchers。而不是。。。。方法:
@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));
}
@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));
}