Issue
I am trying to test a thymeleaf template that returns content based on the user's spring security role.
I am looking to check that some content does not exist
@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")));
Is this possible?
Solution
One Solution is to use hamcrests CoreMatchers.not(....) method:
@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));
}
Answered By - Michael W