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

MockFor/StubFor makes mocked internal method return null

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.5.0, 3.0.7
    • 3.0.8, 4.0.0-alpha-3
    • None
    • None

    Description

      import groovy.mock.interceptor.MockFor
      
      class Helper {
          String string
      
          Helper() {
              string = internalMethod()
          }
      
          private String internalMethod() {
              return helperMethod()
          }
      
          String helperMethod() {
              return 'not intercepted'
          }
      }
      
      MockFor mock = new MockFor(Helper)
      mock.demand.helperMethod { -> 'intercepted' }
      mock.ignore('getString')
      
      mock.use {
          def helper = new Helper()
          assert helper.string == 'intercepted'
      }
      

      method() is a public method, thus I expect it to respect mocking, even for internal calls. Let me know if this expectation is wrong.

      With Groovy 2.4.21, the test works as expected. With Groovy 3.0.7, it fails:

      Assertion failed: 
      
      assert helper.string == 'stub'
             |      |      |
             |      null   false
             Helper@5c00384f
      
      	at test$_run_closure2.doCall(test.groovy:17)
      	at test$_run_closure2.doCall(test.groovy)
      	at test.run(test.groovy:15)
      

      Instead of either the original or mocked return value, we got null instead.

      It looks like it works for call(), but not for callCurrent(). I think the issue is that MockProxyMetaClass overrides one signature of invokeMethod(), but not the one used by callCurrent(). Might 34ad466ba6a be a relevant change?

      Attachments

        Activity

          People

            daniel_sun Daniel Sun
            rtandy Ryan Tandy
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 40m
                40m