Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-2503 MOP 2.0 design inflluencing issues
  3. GROOVY-2282

ProxyMetaClass doesn't work as expected with StubFor/Mockfor

    XMLWordPrintableJSON

Details

    • Sub-task
    • Status: Closed
    • Critical
    • Resolution: Fixed
    • 1.1-rc-2
    • 1.8.0
    • mocks and stubs
    • None

    Description

      Example from the mailing list

      import groovy.mock.interceptor.StubFor
      
      class Speaker {
         String name
         def startLecture() { "Starting..." }
      }
      
      def speakerStub = new StubFor(Speaker)
      speakerStub.demand.startLecture() { "Intercepted!" }
      def speaker1 = new Speaker()
      speakerStub.use {
         def speaker2 = new Speaker()
         assert speaker1.startLecture() == "Starting..." // I would have expected "Intercepted!" here
         assert speaker2.startLecture() == "Intercepted!"
      } 
      

      Dierk's answer:
      MockFor and StubFor work by replacing the MetaClass with a ProxyMetaClass
      and therefore should not be dependend on whether you instantiate the
      object before or inside the use closure.
      If they do, it's a bug (maybe introduced by the latest changes to
      the MOP impl). Please raise an issue.

      I'm flagging this as a blocker because it is MetaClass related and we're on crunch mode.

      Attachments

        1. StubTest.groovy
          2 kB
          Andres Almiray

        Activity

          People

            Unassigned Unassigned
            aalmiray Andres Almiray
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: