将mockMvc与直接bean访问一起使用时,未应用模拟身份验证

时间:2017-06-12 06:13:14

标签: spring spring-security spock spring-test spring-mvc-test

我有一个用 An Authentication object was not found in the SecurityContext 注释的UserRepository。当我执行mockMvc请求时,模拟的身份验证按预期工作。但之后的存储库访问失败了:

@WithMockUser(roles = "ADMIN")
def "access repo with security"() {
    expect:
        mockMvc.perform(get("/"))
        employeeRepository.deleteAll()
        employeeRepository.count() == 0
}

这是我失败的测试:

@WithMockUser(roles = "ADMIN")
def "access repo with security"() {
    expect:
        employeeRepository.deleteAll()
        employeeRepository.count() == 0
}

此测试(没有mockMvc)成功:

calloc

任何人都可以解释一下,为什么它在mockMvc之后失败了?

0 个答案:

没有答案