Is there a way in Groovy to mock a method to accept any number and type of args?

So my use case is that I have method with 12 params, and I have to write it as follows,

testController.testService = [ testMethod: { a, b, c, d, e, f, g, h, i, j, k, m -> null } ]

It seems like there should be a better way to do this.

有帮助吗?

解决方案

testController.testService = [ testMethod: { Object... args -> null } ]

Above would work for varargs, but to match with the signature of service class you can infer the map to that class as:

testController.testService = [ testMethod: { Object... args -> null } ] as TestService

You can also provide the actual type if all of the args are of same type.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top