jira.codehaus.org

  • Log In Access more options
    • Online Help
    • Keyboard Shortcuts
    • About JIRA
    • JIRA Credits
    • What?s New
  • Dashboards Access more options (Alt+d)
  • Projects Access more options (Alt+p)
  • Issues Access more options (Alt+i)
Signup
groovy
  • groovy
  • GROOVY-2630

Support Half-Mocks

  • Log In
  • Views
    • XML
    • Word
    • Printable

Details

  • Type: New Feature New Feature
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.0
  • Fix Version/s: 1.7.1
  • Component/s: mocks and stubs
  • Labels:
    None
  • Testcase included:
    yes
  • Patch Submitted:
    Yes
  • Number of attachments :
    2

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.

  • Options
    • Sort By Name
    • Sort By Date
    • Ascending
    • Descending
    • Download All

Attachments

  1. Text File
    GROOVY-2630__Half_mocks.patch
    03/Feb/10 9:08 AM
    16 kB
    Paul King
  2. Hide
    Zip Archive
    halfmock.zip
    25/Feb/08 12:36 PM
    3 kB
    Stefan Roock
    1. File
      halfmock/HalfMockFor.groovy 3 kB
    2. File
      halfmock/HalfMockProxyMetaClass.groovy 4 kB
    3. File
      halfmock/HalfMockTest.groovy 3 kB
    Download Zip
    Show
    Zip Archive
    halfmock.zip
    25/Feb/08 12:36 PM
    3 kB
    Stefan Roock

Issue Links

is depended upon by

Improvement - An improvement or enhancement to an existing feature or task. GROOVY-4026 MockFor and StubFor improvements

  • Major - Major loss of function.
  • Closed - The issue is considered finished, the resolution is correct. Issues which are not closed can be reopened.

Activity

Ascending order - Click to sort in descending order
  • All
  • Comments
  • Work Log
  • History
  • Activity
Hide
Permalink
Paul King added a comment - 03/Feb/10 8:02 AM

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 - 03/Feb/10 8:02 AM 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
Permalink
Paul King added a comment - 03/Feb/10 9:08 AM

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 - 03/Feb/10 9:08 AM 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.
Hide
Permalink
Paul King added a comment - 07/Feb/10 7:04 AM

Added in trunk

Show
Paul King added a comment - 07/Feb/10 7:04 AM Added in trunk

People

  • Assignee:
    Paul King
    Reporter:
    Stefan Roock
Vote (0)
Watch (1)

Dates

  • Created:
    25/Feb/08 12:36 PM
    Updated:
    07/Apr/10 11:47 PM
    Resolved:
    07/Feb/10 7:04 AM
  • Atlassian JIRA (v5.2.7#850-sha1:b2af0c8)
  • Report a problem
  • Powered by a free Atlassian JIRA open source license for Codehaus. Try JIRA - bug tracking software for your team.