One of the great things about Grails is how easy it is to do integration testing. When running the integration tests, the grails container will be started up, including an in-memory version of hsqldb. (and it looks to go to h2 in 1.4) When you combine that with controller mocking, you can easily test from you controller down to the database. However, because you’re getting your entire system, minus only the web server, it can take a while to startup. It’s not too bad for a CI build, but can get annoying when you’re writing and running tests in your development environment. One of the big culprits in terms of time is spring security. I’m not sure why that is, but it is possible to disable it in your integration test environment. The first step is to set the active configuration property to false in Config.groovy:
Now, when you run ‘grails test-app integration:’ you should see something like this:
The important thing to notice is the last line: Spring Security is disabled, not loading
Of course, there’s now a new problem, any code you have that is referencing springSecurityService will now fail. In general, the most likely method to cause an issue is getCurrentUser(). However, it’s simple to create a quick stub for it:
In my case, I only needed to stub getCurrentUser() and encodePassword, however, you may need to add more, and the general approach can be applied to any additional methods you may need. It is also worth noting that I debated about using something like stubFor, but decided it was more straightforward to make a simple stub.
Now that you have a stub, you need to make sure that it’s being set in the test environment. You can do this by adding the following to resources.groovy:
Now for the last step, setting up the current user in your bootstrap:
Any code running during an integration test, will now get back the saved user if it calls getCurrentUser()
test {
grails.plugins.springsecurity.active = false
}
Now, when you run ‘grails test-app integration:’ you should see something like this:
grails test-app integration:
Welcome to Grails 1.3.6 - http://grails.org/
Licensed under Apache Standard License 2.0
Resolving dependencies...
Dependencies resolved in 5123ms.
Environment set to test
Starting integration test phase ...
Spring Security is disabled, not loading
The important thing to notice is the last line: Spring Security is disabled, not loading
Of course, there’s now a new problem, any code you have that is referencing springSecurityService will now fail. In general, the most likely method to cause an issue is getCurrentUser(). However, it’s simple to create a quick stub for it:
class StubSpringSecurityService {
def currentUser
Object getCurrentUser() {
return currentUser
}
String encodePassword(String password, salt = null) {
return password
}
}
In my case, I only needed to stub getCurrentUser() and encodePassword, however, you may need to add more, and the general approach can be applied to any additional methods you may need. It is also worth noting that I debated about using something like stubFor, but decided it was more straightforward to make a simple stub.
Now that you have a stub, you need to make sure that it’s being set in the test environment. You can do this by adding the following to resources.groovy:
if (GrailsUtil.environment == "test"){
springSecurityService(StubSpringSecurityService)
}
Now for the last step, setting up the current user in your bootstrap:
if (GrailsUtil.environment == "test") {
def testUser = new User()
testUser.save(failOnError: true)
springSecurityService.currentUser = testUser;
}
Any code running during an integration test, will now get back the saved user if it calls getCurrentUser()
Now, I want to enable the springsecurity plugin when I am running functional test using selenium RC , how to do it ? But, I want it to be disabled during the unit and integration test.
2011-09-22 21:15:44,831 [main] ERROR spring.GrailsRuntimeConfigurator - [RuntimeConfiguration] Unable to load beans from resources.groovy
groovy.lang.MissingPropertyException: No such property: org for class: resources
What could be the reason?