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

Partial mocking static method calls with Ignore & Demand don't work as expected.

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 2.5.7
    • None
    • mocks and stubs
    • None

    Description

      When defining a mockFor on a class with static methods, if you ignore a method which invokes a demand mocked method. The demand is not intercepted.

      Here is an example modified from the MockFor documentation:

       

      import groovy.mock.interceptor.MockFor
      
      class Person {
        static def ignoreMe(foo) { foo }
        static def ignoreMeToo() { ignoreMe('foo') }
        static def ignoreMeThree() { ignoreMe('foo') }
      }
      
      def mock = new MockFor(Person)
      mock.ignore('ignoreMeToo')
      mock.ignore('ignoreMeThree')
      mock.demand.ignoreMe { s -> 'baz' }
      
      mock.use {
        assert Person.ignoreMe('foo') == 'baz'
        assert Person.ignoreMeToo() == 'baz'
        assert Person.ignoreMeThree() == 'baz'
      }
      

       

      Running this will result in a failure as Person.ignoreMeToo() returns `foo` instead of `baz`.

      Use Case: Mocking nested static method calls. I want to test a particular method. I know/expect that method to invoke another static method on the same class, but I want to mock that one, and return an expected value.

      Attachments

        Activity

          People

            Unassigned Unassigned
            bstopp Bryan Stopp
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: