/ Published in: Groovy
http://jira.codehaus.org/browse/GROOVY-2630
Expand |
Embed | Plain Text
package halfmock import groovy.lang.*; import java.beans.IntrospectionException; /** * The ProxyMetaClass for the MockInterceptor. * Instance and class methods are intercepted, but constructors are not to allow mocking of aggregated objects. * @author Stefan Roock (based on MockProxyMetaClass by Dierk Koenig) */ def registeredInterceptor /** * @param adaptee the MetaClass to decorate with interceptability */ public HalfMockProxyMetaClass(MetaClassRegistry registry, Class theClass, MetaClass adaptee) throws IntrospectionException { def clazz = theClass clazz = clazz.superclass } } /** * convenience factory method for the most usual case. */ MetaClassRegistry metaRegistry = GroovySystem.getMetaClassRegistry(); MetaClass meta = metaRegistry.getMetaClass(theClass); } } } return doWithoutInterceptor { } } } } return doWithoutInterceptor { } } } } else { return doWithoutInterceptor { } } } } } else { doWithoutInterceptor { } } } /** * Unlike general impl in superclass, ctors are not intercepted but relayed */ } registeredInterceptor = interceptor interceptor = null restoreInterceptor() result } interceptor = interceptor ?: registeredInterceptor return interceptor } } package halfmock import groovy.mock.interceptor.* /** Facade over the Mocking details. A Mock's expectation is always sequence dependent and it's use always ends with a verify(). See also StubFor. @author Stefan Roock (based on MockFor from Dierk Koenig and Paul King */ class HalfMockFor { HalfMockProxyMetaClass proxy Demand demand def expect Class clazz proxy = HalfMockProxyMetaClass.make(clazz) } proxy.use closure expect.verify() } proxy.use obj, closure expect.verify() } instanceExpectations[obj].verify() } } instance.metaClass = thisproxy instanceExpectations[instance] = thisexpect return instance } } instance.metaClass = thisproxy wrapped = ProxyGenerator.instantiateDelegate([clazz], instance) wrapped = ProxyGenerator.instantiateDelegate(instance) } instanceExpectations[wrapped] = thisexpect return wrapped } instance = ProxyGenerator.instantiateAggregateFromInterface(clazz) instance = ProxyGenerator.instantiateAggregateFromBaseClass(clazz, args) instance = clazz.newInstance(args) instance = ProxyGenerator.instantiateDelegate(clazz.newInstance(args)) } instance = clazz.newInstance() instance = ProxyGenerator.instantiateDelegate(clazz.newInstance()) } } return instance } } // Test package halfmock import groovy.mock.interceptor.* class Foo { def existingProperty = 0 Foo() { constructorCalls++ } existsCalls++ } doesntExist() } staticExistsCalls++ } } } Foo.constructorCalls = 0 Foo.staticExistsCalls = 0 Foo.existsCalls = 0 } mock.use { } assertEquals 1, Foo.constructorCalls } mock.demand.doesntExist() {} mock.use { foo.doesntExist() } } mock.use { foo.exists() } assertEquals 1, Foo.existsCalls } mock.demand.doesntExist() {} mock.use { foo.callsDoesntExist() } } mock.demand.staticDoesntExist() {} mock.use { Foo.staticDoesntExist() } } mock.use { Foo.staticExists() } assertEquals 1, Foo.staticExistsCalls } mock.demand.setNonExistingProperty(1) {} mock.demand.getNonExistingProperty() {2} mock.use { foo.nonExistingProperty = 1 assertEquals 2, foo.nonExistingProperty } } mock.use { foo.existingProperty = 1 assertEquals 1, foo.existingProperty } } mock.use { bar.existingProperty = 1 assertEquals 1, bar.existingProperty } } }
You need to login to post a comment.
