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
Up to Spec: JavaScript Numeric Separators
Let's take a look at the proposal to add Numeric Separators to the JavaScript specification.
Using Conftest to Validate Configuration Files
Conftest is a utility within the Open Policy Agent ecosystem that helps simplify writing validation tests against configuration files. In a previous blog post, I wrote about using the Open Policy Agent utility directly to […]
SwiftGen with Image & Color Asset Catalogs
You might remember back in 2015 when iOS 9 was introduced, and we were finally given a way to manage all of our assets in one place with Asset Catalogs. A few years later, support […]
Tracking Original URL Through Authentication
If you read my other post about refreshing AWS tokens, then you probably have a use case for keeping track of the original requested resource while the user goes through authentication so you can route […]