Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc Spring MockMvc不期望内容_Spring Mvc_Spring Security_Spring Test - Fatal编程技术网

Spring mvc Spring MockMvc不期望内容

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

我正在尝试测试一个thymeleaf模板,该模板根据用户的spring安全角色返回内容

我希望检查一些内容是否不存在

@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));
}