Feb 8, 2013

Grails testing – mocking 3rd-party services

When working with external services in a Grails project, say a service that uploads files to Amazon S3 or verifies bank account information, you’ll likely want to avoid accessing these services in standard integration and functional tests. Hitting these services exposes your tests to failures caused by the service being slow or being down all together, or a whole host of other failures that are unrelated to you code. Not to mention some services, like the aforementioned bank account verification service, may have a per-use fee. You don’t want to get charged each time you run your normal test suite.

If the code that accesses the third-party service is isolated into a Grails service, it’s fairly easily to override those services in an integration test. But how to avoid hitting the ‘real’ third-party web service in a functional test?

One way to keep your tests isolated to your code and avoid a dependence on a third-party service is to create a mock implementation of your the Grails service and use Grails dependency injection to override that service bean in the test environment.

And in the resources.groovy bean definitions, explicitly override that service in the test environment.

Now, since this mock service has to live in the actual codebase for the bean override to work (under grails-app/services in this example), the mock service will by included in the war file by default. You don’t want this mock service in the production war, so one way to exclude is to explicitly delete it’s package from the war:

Then you can include all your mock services in one package, and each subsequent mock service will be removed from the production war.

And the functional test that uses the mock S3 file upload service could look something like this:

This is an example of the convenience of combining bean overriding with Grails environment-specific code to mock out third-party services.

About the Author

Object Partners profile.

One thought on “Grails testing – mocking 3rd-party services

  1. Tomas says:

    You can also use betamax, which is a much nicer solution for this – http://freeside.co/betamax/

  2. Craig Atkinson says:

    Thanks for mentioning the Betamax library – it looks like convenient way to mock out full web service calls.

  3. Anders says:


    Nice example. Can you explain in a little more detail why you don’t want the mock services included in your WAR-file? They seem harmless to me when not configured in resources.groovy


Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
Android Development for iOS Developers
Android development has greatly improved since the early days. Maybe you tried it out when Android development was done in Eclipse, emulators were slow and buggy, and Java was the required language. Things have changed […]
Add a custom object to your Liquibase diff
Adding a custom object to your liquibase diff is a pretty simple two step process. Create an implementation of DatabaseObject Create an implementation of SnapshotGenerator In my case I wanted to add tracking of Stored […]
Keeping Secrets Out of Terraform State
There are many instances where you will want to create resources via Terraform with secrets that you just don’t want anyone to see. These could be IAM credentials, certificates, RDS DB credentials, etc. One problem […]
Validating Terraform Plans using Open Policy Agent
When developing infrastructure as code using terraform, it can be difficult to test and validate changes without executing the code against a real environment. The feedback loop between writing a line of code and understanding […]