Details
Description
The following example shows the problem mocking JSecurity's SecurityUtils.
In the controller action, you have a call to the SecurityUtils:
def myAction = {
...
SecurityUtils.subject.isAuthenticated()
...
}
Your test class:
class MyTests extends ControllerUnitTestCase { def securityMock def controller protected void setUp() { super.setUp() securityMock = mockFor(SecurityUtils) securityMock.demand.static.getSubject{-> [isAuthenticated:{false}] as Subject} controller = MyController() } void testOne() { controller.myAction() } void testTwo() { controller.myAction() } }
Expectation: Tests succeed, because getSubject() should be mocked for each test with an expectation of one call.
But: First Test succeed, second test fails with: "No more calls to 'getSubject' expected at this point. End of demands."
Workaround: Specify "securityMock.demand.static.getSubject(1..2){-> [isAuthenticated:{false}] as Subject}". Not practical, because setUp() has to have insides about all tests in the class.
Activity
- All
- Comments
- Work Log
- History
- Activity
- Git Commits
This is related to callsite caching. Graeme reported this bug http://jira.codehaus.org/browse/GROOVY-3433 and using the Groovy 1.6.1 jar fixes this problem too. Note that because of hard-coded jar names, you need to rename groovy-all-1.6.1.jar to groovy-all-1.6.0.jar in $GRAILS_HOME/lib