groovy
  1. groovy
  2. GROOVY-2271

Mocks are not used by the delegate of a closure

    Details

    • Testcase included:
      yes
    • Number of attachments :
      2

      Description

      The following script leads to an exception. The closure does not delegate to the Mock.

      import groovy.mock.interceptor.StubFor
      
      class Foo {
          def createBar() {
              throw new RuntimeException("We should never get here!")
          }
      }
      
      StubFor fooStub = new StubFor(Foo)
      
      fooStub.demand.createBar { "I'm a mock" }
      
      Closure closure = { createBar() }
      
      fooStub.use {
          Foo foo = new Foo()
          assert "I'm a mock" == foo.createBar()
          closure.delegate = foo
          assert "I'm a mock" == closure.call()
      }
      
      1. 2271Patch_v16X.txt
        0.9 kB
        Roshan Dawrani
      2. MockClosureTest.groovy
        0.8 kB
        Hans Dockter

        Activity

        Hide
        Paul King added a comment -

        Slight variation:

        import groovy.mock.interceptor.StubFor
        class Foo {
          def createBar() { throw new RuntimeException("We should never get here!") }
        }
        def fooStub = new StubFor(Foo)
        fooStub.demand.createBar(3..3) { "I'm a mock" }
        
        fooStub.use {
          def foo = new Foo()
          assert "I'm a mock" == foo.createBar()
        
          def closure = { foo.createBar() }
          assert "I'm a mock" == closure.call()
        
          closure = { createBar() }
          closure.delegate = foo
          assert "I'm a mock" == closure.call()
        }
        fooStub.expect.verify()
        
        Show
        Paul King added a comment - Slight variation: import groovy.mock.interceptor.StubFor class Foo { def createBar() { throw new RuntimeException( "We should never get here!" ) } } def fooStub = new StubFor(Foo) fooStub.demand.createBar(3..3) { "I'm a mock" } fooStub.use { def foo = new Foo() assert "I'm a mock" == foo.createBar() def closure = { foo.createBar() } assert "I'm a mock" == closure.call() closure = { createBar() } closure.delegate = foo assert "I'm a mock" == closure.call() } fooStub.expect.verify()
        Hide
        Roshan Dawrani added a comment -

        Hi,
        I am attaching a fix patch for this issue.

        When a closure uses a delegate and if that closure is used in mock/stub closures, ClosureMetaClass was not going through the delegate's metaClass's (MockProxyMetaClass) invokeMethod(), effectively bypassing the mock/stub interception callbacks to MockInterceptor.

        Fixed to relay the call to method through delegate's metaClass's invokeMethod() so that mocking gets used. Verified with the test case attached with JIRA.

        rgds,
        Roshan

        Show
        Roshan Dawrani added a comment - Hi, I am attaching a fix patch for this issue. When a closure uses a delegate and if that closure is used in mock/stub closures, ClosureMetaClass was not going through the delegate's metaClass's (MockProxyMetaClass) invokeMethod(), effectively bypassing the mock/stub interception callbacks to MockInterceptor. Fixed to relay the call to method through delegate's metaClass's invokeMethod() so that mocking gets used. Verified with the test case attached with JIRA. rgds, Roshan
        Hide
        Roshan Dawrani added a comment -

        Fixed

        Show
        Roshan Dawrani added a comment - Fixed
        Hide
        Paul King added a comment -

        Roshan rocks!

        Show
        Paul King added a comment - Roshan rocks!
        Hide
        Guillaume Laforge added a comment -

        Of course he rocks

        Show
        Guillaume Laforge added a comment - Of course he rocks
        Hide
        Roshan Dawrani added a comment -

        You guys are scaring me !

        Show
        Roshan Dawrani added a comment - You guys are scaring me !

          People

          • Assignee:
            Roshan Dawrani
            Reporter:
            Hans Dockter
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: