Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top