Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is SpringSecurityUtils null in this Grails unit test?

In Grails 2.0.4, I'm trying to write a controller unit test which invokes the static SpringSecurityUtils.reauthenticate. The test returns a NullPointerException on that invocation. In a debugger, I can see that none of the Groovy dynamic properties (declaredMethods,etc.) of SpringSecurityUtils are populated.

I do note that when running the tests, the "Configuring Spring Security Core" log message is emitted after the unit-test failure. Here is a sample test:

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
    }
}

My initial reaction is that maybe plugins aren't accessible during unit tests, but if so, why is the URLUtils call working? And why does the test get "far enough" to initialize the plugin, but after the tests have completed?

like image 898
withoutairs Avatar asked May 12 '26 07:05

withoutairs


1 Answers

For a unit test the container isn't starting up. No Spring injection or "grails goodness" is happening. You see in the logs that the plugin is initializing after the unit tests run, because it [container] does start for the integration tests. If you want to test the SpringSecurityUtils, although guessing it is already tested properly in the plugin, you would want to write an integration test.

like image 191
Nick Larson Avatar answered May 15 '26 00:05

Nick Larson