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()
3 comments:
Hi , I am getting the following error when I followed the steps your mentioned.
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?
Tough to say. It looks like there may be an issue with the code you added to your resources.groovy. Can you paste that into a comment?
I solved this problem. The problem is I was having my Stub file under test/integration directory.
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.
Post a Comment