开发者

Stubbing all methods of a groovy class?

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜