Is there an easy way to stub ALL methods of a groovy class? In one of my tests, I need to make sure a certain code path doesn't touch a service at all.
That is, I want to mock every method like this:
[meth1: {-> fail(msg)},
meth2: {-> fail(msg)...}] as MyService
I开发者_高级运维s there an easy way to stub and intercept all methods of all argument types and perform some uniform action like this?
The most simple way I can think of is something like this:
MyService.metaClass.invokeMethod { String name, args ->
assert false
}
If MyService
is an interface you can do this:
MyService stub = {Object[] args -> fail(msg)} as MyService
I'm not sure if this works when MyService
is a class
精彩评论