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.

Related Blog Posts
Natively Compiled Java on Google App Engine
Google App Engine is a platform-as-a-service product that is marketed as a way to get your applications into the cloud without necessarily knowing all of the infrastructure bits and pieces to do so. Google App […]
Building Better Data Visualization Experiences: Part 2 of 2
If you don't have a Ph.D. in data science, the raw data might be difficult to comprehend. This is where data visualization comes in.
Unleashing Feature Flags onto Kafka Consumers
Feature flags are a tool to strategically enable or disable functionality at runtime. They are often used to drive different user experiences but can also be useful in real-time data systems. In this post, we’ll […]
A security model for developers
Software security is more important than ever, but developing secure applications is more confusing than ever. TLS, mTLS, RBAC, SAML, OAUTH, OWASP, GDPR, SASL, RSA, JWT, cookie, attack vector, DDoS, firewall, VPN, security groups, exploit, […]