Grails

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

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Critical Critical
  • Resolution: Fixed
  • Affects Version/s: 1.1
  • Fix Version/s: 1.1.1
  • Component/s: Testing
  • Labels:
    None
  • Environment:
    1.1

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

Hide
Burt Beckwith added a comment -

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

Show
Burt Beckwith added a comment - 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

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: