Groovy
  1. Groovy
  2. GROOVY-1823

Add ability to mock/stub constructors

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.0
    • Fix Version/s: 1.7.1
    • Component/s: mocks and stubs
    • Labels:
      None
    • Environment:
      groovy 1.0, Java 1.5, Windows 2000

      Description

      Would like the ability to stub/mock constructors.

      Example:

      def dateStub = new StubFor( Date )

      dateStub.demand.Date

      { aControlCurrentTime }

      Expectation is that .Date would be the default constructor of the Date class.

        Activity

        David Weiler-Thiessen created issue -
        Paul King made changes -
        Field Original Value New Value
        Component/s mocks and stubs [ 13113 ]
        Hide
        Paul King added a comment -

        I have a minor modification to MockFor/StubFor which supports this functionality. Here is an example of usage (mirroring this issue's suggestion):

        import groovy.mock.interceptor.MockFor
        class Person {
          String first, last
        }
        def interceptConstructorCalls = true
        def mock = new MockFor(Person, interceptConstructorCalls)
        def dummy = new Person(first:'Tom', last:'Jones')
        mock.demand.with {
          Person() { dummy } // expect constructor call, return dummy
          getFirst() {'John'}
          getLast() {'Doe'}
        }
        mock.use {
          def p = new Person(first:'Mary', last:'Smith')
          assert p.first == 'John'
          assert p.last == 'Doe'
        }
        

        There is an optional parameter (defaulting to false) for the Constructor which indicates that mocking of the constructor is to be enabled. It is only possible to use with GroovyObjects.

        Show
        Paul King added a comment - I have a minor modification to MockFor/StubFor which supports this functionality. Here is an example of usage (mirroring this issue's suggestion): import groovy.mock.interceptor.MockFor class Person { String first, last } def interceptConstructorCalls = true def mock = new MockFor(Person, interceptConstructorCalls) def dummy = new Person(first:'Tom', last:'Jones') mock.demand.with { Person() { dummy } // expect constructor call, return dummy getFirst() {'John'} getLast() {'Doe'} } mock.use { def p = new Person(first:'Mary', last:'Smith') assert p.first == 'John' assert p.last == 'Doe' } There is an optional parameter (defaulting to false) for the Constructor which indicates that mocking of the constructor is to be enabled. It is only possible to use with GroovyObjects.
        Paul King made changes -
        Assignee Paul King [ paulk_asert ]
        Hide
        Paul King added a comment -

        Proposed patch - planning to apply to trunk once I have written some tests.

        Show
        Paul King added a comment - Proposed patch - planning to apply to trunk once I have written some tests.
        Paul King made changes -
        Attachment GROOVY-1823__Add_ability_to_mock_stub_constructors.patch [ 47334 ]
        Hide
        Paul King added a comment -

        Fixed in trunk.

        Show
        Paul King added a comment - Fixed in trunk.
        Paul King made changes -
        Fix Version/s 1.7.1 [ 16014 ]
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Fixed [ 1 ]
        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 [ 12730939 ] Default workflow, editable Closed status [ 12742847 ]
        Mark Thomas made changes -
        Project Import Mon Apr 06 02:11:23 UTC 2015 [ 1428286283443 ]
        Mark Thomas made changes -
        Workflow jira [ 12969184 ] Default workflow, editable Closed status [ 12976964 ]
        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
        1034d 21h 6m 1 Paul King 07/Feb/10 07:02
        Resolved Resolved Closed Closed
        59d 16h 44m 1 Paul King 08/Apr/10 00:46

          People

          • Assignee:
            Paul King
            Reporter:
            David Weiler-Thiessen
          • Votes:
            5 Vote for this issue
            Watchers:
            8 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development