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
An Exploration in Rust: Musings From a Java/C++ Developer
Why Rust? It’s fast (runtime performance) It’s small (binary size) It’s safe (no memory leaks) It’s modern (build system, language features, etc) When Is It Worth It? Embedded systems (where it is implied that interpreted […]
Getting Started with CSS Container Queries
For as long as I’ve been working full-time on the front-end, I’ve heard about the promise of container queries and their potential to solve the majority of our responsive web design needs. And, for as […]
Simple improvements to making decisions in teams
Software development teams need to make a lot of decisions. Functional requirements, non-functional requirements, user experience, API contracts, tech stack, architecture, database schemas, cloud providers, deployment strategy, test strategy, security, and the list goes on. […]
JavaScript Bundle Optimization – Polyfills
If you are lucky enough to only support a small subset of browsers (for example, you are targeting a controlled set of users), feel free to move along. However, if your website is open to […]