黄瓜无法使用春季安全

时间:2015-11-09 19:33:14

标签: java grails spring-security cucumber

我正在尝试在Grails 2.1.5和Java 1.7上使用Cucumber 0.10.0和Geb 0.9.2来测试Spring Security的登录身份验证。尽管使用spring security的登录身份验证正在应用程序上运行,但在测试有效用户(在数据库上)的方案中却没有。这段代码可以更好地解释发生了什么:

Scenario: Log in as a generator
    Given I am at the HomePage
    When I go to the login page
    And Enter the username "ru"
    And The password "pass" at the correct fields
    Then I log in as a generator

Given(~'^I am at the HomePage$'){->
    to HomePage
    at HomePage
}

When(~'^I go to the login page$'){ ->
    page.clickLogIn()
    at LoginAuthenticationPage
}

And(~'^Enter the username "([^"]*)"$'){ String username ->
    page.fillUsername(username)
}

And(~'^The password "([^"]*)" at the correct fields$'){ String password ->
    page.fillPassword(password)
}

Then(~'^I log in as a generator$'){->
    page.logInButtonClick()
    at GeneratorDashboardPage
    page.logOff()
}

在“然后”步骤,当方法

page.logInButtonClick() 
调用

并尝试确认登录验证发生此错误:

Error |
2015-11-09 15:44:22,900 [http-bio-8070-exec-8] ERROR errors.GrailsExceptionResolver  - NullPointerException occurred when processing request: [GET] /ResS/dashboard
Cannot invoke method getAuthorities() on null object. Stacktrace follows:
Message: Cannot invoke method getAuthorities() on null object

此方法getAuthorities()来自控制器类,用于决定登录时此类用户的重定向位置:

class DashboardController {

    def springSecurityService;

    def index() {
         User user = (User)springSecurityService.currentUser;
            if(user.getAuthorities().contains(Role.findByAuthority('ROLE_GENERATOR')))
                redirect(controller: "generatorDashboard", action: "index")
            if(user.getAuthorities().contains(Role.findByAuthority('ROLE_COLLECTOR')))
                redirect(controller: "collectorDashboard", action: "index")
    }

}

我注意到了

def springSecurityService

依赖性未被注入,因此对象

User user = (User)springSecurityService.currentUser;

为空。任何人都知道为什么没有注入依赖?我想也许这个Cucumber版本不支持这种类型的测试。

0 个答案:

没有答案