Closed GoogleCodeExporter closed 8 years ago
There is now a simple example in the 1.0-Snapshot docs [1,2].
However, there is a caveat: metaclass changes are only 'confined' for NEW
instances created within the scope of the annotated method/class.
For example: in a grails integration spec, modifications made to an injected
service's metaclass will not be reverted after the spec.
@ConfineMetaClassChanges([UserService])
class MySpec extends IngegrationSpec {
def userService
void setupSpec() {
// This does not get cleaned up after the spec !!!
userService.metaClass.findUserById = { id -> new User('joe') }
}
void "foo should do bar"() {
// ...
}
}
More Details:
@ConfineMetaClassChanges takes a snapshot of the metaclasses for the specified
classes before entering the annotated scope, and restores them afterwards,
using groovy's MetaClassRegistry.
[1] https://github.com/spockframework/spock/pull/30#issuecomment-31571598
[2]
http://docs.spockframework.org/en/latest/extensions.html#confinemetaclasschanges
Original comment by David.W....@gmail.com
on 5 Jan 2014 at 7:47
Thanks, closing.
Original comment by pnied...@gmail.com
on 5 Jan 2014 at 10:44
Original issue reported on code.google.com by
raviteja...@gmail.com
on 23 Dec 2011 at 11:37