When unit testing services in Grails and Spock you can run into issues when your service logs messages. The problem is that Spock doesn’t automatically add the log property to your service. You’ll find most of the answer here but it’s not complete and doesn’t work as is. Most of the time you also don’t need the actual log messages, you’re testing the code. So, here’s the simple solution. Add this method to your Spock test class:
Now, in your test setup, just call the method
mockLog(myService). If you don’t want the log messages littering your output, then replace the
println with no-op calls.
Categories: JavaGrails, Spock