我有一个控制器方法,我像这样注释:
@Secured(['ROLE_ADMIN'])
def save() {
... // code ommitted
}
我正在尝试编写一个单元测试来验证只有管理员用户可以点击 URL:
def "Only the admin user should be able to invoke save"() {
given:
def user = createNonAdminUser() // let's pretend this method exists
controller.springSecurityService = Mock(SpringSecurityService)
controller.springSecurityService.currentUser >> user
when:
controller.save()
then:
view ==~ 'accessdenied'
}
但是,返回的视图是save
视图而不是拒绝访问视图。看起来它@Secured
完全绕过了注释。有没有办法@Secured
从单元测试或集成测试中测试注释?