Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-2271

Mocks are not used by the delegate of a closure

    XMLWordPrintableJSON

Details

    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()
      }
      

      Attachments

        1. MockClosureTest.groovy
          0.8 kB
          Hans Dockter
        2. 2271Patch_v16X.txt
          0.9 kB
          Roshan Dawrani

        Activity

          People

            roshandawrani Roshan Dawrani
            hans_d Hans Dockter
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: