Expectation is that .Date would be the default constructor of the Date class.
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.
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 added a comment - 03/Feb/10 05:10 AM 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.
I have a minor modification to MockFor/StubFor which supports this functionality. Here is an example of usage (mirroring this issue's suggestion):
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.