3

在 Grails 2.0.4 中,我正在尝试编写一个调用静态 SpringSecurityUtils.reauthenticate 的控制器单元测试。测试在该调用上返回 NullPointerException。在调试器中,我可以看到没有填充 SpringSecurityUtils 的 Groovy 动态属性(declaredMethods 等)。

我确实注意到,在运行测试时,单元测试失败后会发出“配置 Spring Security Core”日志消息。这是一个示例测试:

class ReproTest {
    void testSpringSecurityUtils() {
        String.valueOf(true) // OK: a public final class from the JDK
        URLUtils.isRelativeURL("foo")  // OK: a class from another plugin
        SpringSecurityUtils.reauthenticate "user", "pw" // fails, NPE
    }
}

我最初的反应是,在单元测试期间可能无法访问插件,但如果是这样,为什么 URLUtils 调用有效?为什么测试“足够远”来初始化插件,但是在测试完成之后?

4

1 回答 1

3

对于单元测试,容器没有启动。没有 Spring 注入或“grails goodness”正在发生。您在日志中看到插件在单元测试运行后正在初始化,因为它 [容器] 确实为集成测试启动。如果您想测试 SpringSecurityUtils,虽然猜测它已经在插件中正确测试,但您可能需要编写一个集成测试。

于 2012-07-06T13:33:15.837 回答