Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stub dynamic finder using spock

I need help with a Spock test. I am trying to stub a Domain Object's dynamic finder (findById). I can't use code like:

ObjectDomain.metaClass.static.findById = { -> new ObjectDomain()}

because I use the findsById method in other part of the test and if I use that I get false positives.

anybody knows the best way to stub dynamic finders using Spock?

Thanks in advance.

like image 219
xala3pa Avatar asked Mar 18 '23 17:03

xala3pa


1 Answers

The argument number and types have to match between your metaclass method and the real method. You added a no-arg findById() method but you're calling an overloaded method with an id and a Map. So you'd need to change the closure args to match:

ObjectDomain.metaClass.static.findById = { id, Map args -> new ObjectDomain()}
like image 194
Burt Beckwith Avatar answered Mar 28 '23 20:03

Burt Beckwith