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:

    Hello,

    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

    Best,
    Anders

Leave a Reply

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

Related Blog Posts
React Server Components
The React Team recently announced new work they are doing on React Server Components, a new way of rendering React components. The goal is to create smaller bundle sizes, speed up render time, and prevent […]
Jolt custom java transform
Jolt is a JSON to JSON transformation library where the transform is defined in JSON. It’s really good at reorganizing the json data and massaging it into the output JSON you need. Sometimes, you just […]
Page Object Model for UI Testing
How to make a multi-handled range input
The HTML range input is a great way to allow your users to manipulate a numeric value using their mouse. The range input that is currently provided by browsers only supports a single handle, which […]