Partial Mocks with Spock on Groovy/Grails

Post by Matt Nohr

Jun

15

2012

The Spock testing framework currently does not support partial mocks. It is easy to mock out a class, but not individual methods in a class.

In almost all cases, this makes sense to me. If you really only want to mock out only one method, maybe you need to refactor the code to create a new class. While this is good in theory, it is not always practically possible.

In these cases, you could use Groovy’s meta-programming to accomplish partial mocks.
Let’s say you have a simple class like this:

Notify me of followup comments via e-mail. You can also subscribe without commenting.

At refactr we believe in the value of connection, the utility of agile processes, and the power of great ideas. We are creating the next generation of software for people who expect more from their web applications.