Issue Details (XML | Word | Printable)

Key: GRAILS-4271
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Critical Critical
Assignee: Graeme Rocher
Reporter: Mos
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
Grails

Unit-Test's mockFor isn't unregistered for static methods

Created: 15/Mar/09 07:06 AM   Updated: 01/May/09 07:21 AM   Resolved: 01/May/09 07:21 AM
Component/s: Testing
Affects Version/s: 1.1
Fix Version/s: 1.1.1

Time Tracking:
Not Specified

Environment: 1.1


 Description  « Hide

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.



Burt Beckwith added a comment - 18/Apr/09 09:46 AM

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