I have more general question. Which framework or implementation I should use for mocking in Grails 2.x when using Spock?
I know tons of mocking style: leverage Groovy metaClass, Grails mockFor(), Groovy Mock(), Groovy closure style, etc. Each of them has its own advantages and disadvantages. But what I don't understand is that some mocking style works in certain occasions which I cannot determine (i.e. mockFor() works for certain implementation and not for the others).
Currently I have two similar implementation of service method mocking.
This one works:
@TestFor(MyController)
@Mock([MyDevice])
class MyControllerSpec extends ControllerSpec {
void "test st."() {
def myService = mockFor(MyService)
myService.demand.myMethod() { def st ->
return "test"
}
controller.myService = myService.createMock()
}
}
However, this implementation doesn't work:
@TestFor(MyController)
@Mock([MyDevice])
class MyControllerSpec extends ControllerSpec {
void "test st."() {
def yourService = mockFor(YourService)
yourService.demand.yourMethod() { def st ->
return "test"
}
controller.yourService = yourService.createMock()
}
}
The service implementation and calling from controller is quite similar. So what is the best practice of mocking in Grails? Or is there any GOOD mocking framework for Grails which would save my time figuring out how to mock?
Thanks for any advice! :-)
Mateo