I’m writing functional tests (geb + spock) for a Grails application, and want to have the app resolve a “mock” implementation of a Service when the app loads at test-time.
The ‘real’ version of the service communicates with a remote web serivce.
Ideally I’d have a mock implementation of this service that returns mock data (rather than going to the real, remote web service), and have this service be injected during the functional test runs.
Currently, the service is injected into controllers based on the standard naming convention technique, like so:
class BikeController {
def myService
def index = { render myService.doSomething() as JSON }
}
Is it possible to have something in my config, like:
environments {
test {
/* register myMockService to be used where myService normally would */
}
}
(During unit testing I simply replace methods and do metaClass magic to accomplish these goals)
If I need to create an interface, OK, but still, how to wire it all up?
Thanks!
There’s no support for an
environmentsblock ingrails-app/conf/spring/resources.groovybut you can do an explicit environment check:Note that the service won’t be transactional, but if you need it to be it can be wrapped in a transactional proxy like Grails does for regular services. It might be simpler to just make transactional calls inside of a
withTransactionblock though since this is for testing only.