开发者

Stubbing all methods of a groovy class?

开发者 https://www.devze.com 2023-03-04 17:23 出处:网络
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.

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

精彩评论

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

关注公众号