Details

    • Type: New Feature New Feature
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.0
    • Fix Version/s: 1.7.1
    • Component/s: mocks and stubs
    • Labels:
      None
    • Flags:
      Patch

      Description

      Half-Mocks are mocks for classes with dynamic methods or properties. One example are Grails domain objects where you would use the implemented domain specific methods but want to mock the GORM methods.

      1. GROOVY-2630__Half_mocks.patch
        16 kB
        paulk_asert
      2. halfmock.zip
        3 kB
        Stefan Roock

        Issue Links

          Activity

          Stefan Roock created issue -
          Paul King made changes -
          Field Original Value New Value
          Assignee Paul King [ paulk_asert ]
          Hide
          Paul King added a comment -

          OK, I now have a patch (borrowing from Stefan's patch and also the recent ignore amendments) which supports a form of half-mocks. It needs further testing and doco before committing to trunk but to give a flavor:

          import groovy.mock.interceptor.MockFor
          class Person {
            String first, last
            def name() { "$first $last" }
            def ignoreMe() { 'baz' }
            def ignoreMeToo() { ignoreMe() }
            def ignoreMeThree() { ignoreMe() }
          }
          def mock = new MockFor(Person)
          mock.ignore(~'get.*')
          mock.ignore('ignoreMeToo') { 'boo' }
          mock.ignore(~'ignoreMe.*')
          mock.demand.name{ 'John' }
          mock.use {
            def p = new Person(first:'Mary', last:'Smith')
            assert p.first == 'Mary'
            assert p.last == 'Smith'
            assert p.name() == 'John'
            assert p.ignoreMe() == 'baz'
            assert p.ignoreMeToo() == 'boo'
            assert p.ignoreMeThree() == 'baz'
          }
          

          Basically, all method calls must match according to the appropriate expectation unless explicitly ignored. When ignoring, a closure can be provided to return a value (much like a demand return result) but the call won't be included when comparing with expected calls. If the ignore method has no return closure, then it defaults through to calling the original underlying instance.

          Show
          Paul King added a comment - OK, I now have a patch (borrowing from Stefan's patch and also the recent ignore amendments) which supports a form of half-mocks. It needs further testing and doco before committing to trunk but to give a flavor: import groovy.mock.interceptor.MockFor class Person { String first, last def name() { "$first $last" } def ignoreMe() { 'baz' } def ignoreMeToo() { ignoreMe() } def ignoreMeThree() { ignoreMe() } } def mock = new MockFor(Person) mock.ignore(~'get.*') mock.ignore('ignoreMeToo') { 'boo' } mock.ignore(~'ignoreMe.*') mock.demand.name{ 'John' } mock.use { def p = new Person(first:'Mary', last:'Smith') assert p.first == 'Mary' assert p.last == 'Smith' assert p.name() == 'John' assert p.ignoreMe() == 'baz' assert p.ignoreMeToo() == 'boo' assert p.ignoreMeThree() == 'baz' } Basically, all method calls must match according to the appropriate expectation unless explicitly ignored. When ignoring, a closure can be provided to return a value (much like a demand return result) but the call won't be included when comparing with expected calls. If the ignore method has no return closure, then it defaults through to calling the original underlying instance.
          Hide
          Paul King added a comment -

          Proposed patch (sans complete tests) attached. Sorry for noise as GROOVY-1823 is merged in this patch. Note that the relaying from an ignored method to a non-existing demanded method is not supported as it was in Stefan's patch. I am currently regarding that as an anti-feature but am open to being persuaded that is not the case.

          Show
          Paul King added a comment - Proposed patch (sans complete tests) attached. Sorry for noise as GROOVY-1823 is merged in this patch. Note that the relaying from an ignored method to a non-existing demanded method is not supported as it was in Stefan's patch. I am currently regarding that as an anti-feature but am open to being persuaded that is not the case.
          Paul King made changes -
          Attachment GROOVY-2630__Half_mocks.patch [ 47344 ]
          Paul King made changes -
          Link This issue is depended upon by GROOVY-4026 [ GROOVY-4026 ]
          Hide
          Paul King added a comment -

          Added in trunk

          Show
          Paul King added a comment - Added in trunk
          Paul King made changes -
          Status Open [ 1 ] Resolved [ 5 ]
          Resolution Fixed [ 1 ]
          Paul King made changes -
          Fix Version/s 1.7.1 [ 16014 ]
          Paul King made changes -
          Status Resolved [ 5 ] Closed [ 6 ]
          Mark Thomas made changes -
          Project Import Sun Apr 05 13:32:57 UTC 2015 [ 1428240777691 ]
          Mark Thomas made changes -
          Workflow jira [ 12731666 ] Default workflow, editable Closed status [ 12743543 ]
          Mark Thomas made changes -
          Patch Submitted Yes [ 10763 ]
          Flags Patch [ 10430 ]
          Mark Thomas made changes -
          Project Import Mon Apr 06 02:11:23 UTC 2015 [ 1428286283443 ]
          Mark Thomas made changes -
          Workflow jira [ 12972033 ] Default workflow, editable Closed status [ 12979852 ]
          Mark Thomas made changes -
          Assignee paulk_asert [ paulk_asert ] Paul King [ paulk ]
          Transition Time In Source Status Execution Times Last Executer Last Execution Date
          Open Open Resolved Resolved
          712d 18h 28m 1 Paul King 07/Feb/10 07:04
          Resolved Resolved Closed Closed
          59d 16h 42m 1 Paul King 08/Apr/10 00:47

            People

            • Assignee:
              Paul King
              Reporter:
              Stefan Roock
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development