paint-brush
API Consumer-Contract Tests and Test-Doubles with Karateby@ptrthomas
11,024 reads
11,024 reads

API Consumer-Contract Tests and Test-Doubles with Karate

by Peter ThomasJanuary 7th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

<a href="https://github.com/intuit/karate" target="_blank">Karate</a> recently crossed <a href="https://github.com/intuit/karate/stargazers" target="_blank">500 stars</a> on GitHub and has been very <a href="https://github.com/intuit/karate/wiki/Community-News" target="_blank">well-received</a>, even beginning to feature in <a href="https://twitter.com/KarateDSL/status/935197434191347712" target="_blank">job-postings</a> as a desired skill. Not bad, given this project is just ten months old&nbsp;!

Companies Mentioned

Mention Thumbnail
Mention Thumbnail

Coin Mentioned

Mention Thumbnail
featured image - API Consumer-Contract Tests and Test-Doubles with Karate
Peter Thomas HackerNoon profile picture

Karate recently crossed 500 stars on GitHub and has been very well-received, even beginning to feature in job-postings as a desired skill. Not bad, given this project is just ten months old !

Test Doubles

One of the features in the works (and close to being released) is the ability to spin-up test-doubles to “mock” a web-service API dependency that may still be in development — or for which a test-environment may be un-available or unstable. This is a significant pain-point for many teams — and “flaky integration tests” are legendary sources of pain for many a platform team dealing with micro-services.

Karate’s innovative “mis-use” of Gherkin has worked out extremely well in practice, and actually happens to be a great fit even when “inverting” things to work on the server-side. Here’s an example that showcases how simple it is to write a stateful test-double for a REST service. Things just click into place, like Karate’s native support for JSON and XML, and the power of the embedded JavaScript engine shines through.

the actual source for this example: demo-mock.feature

You can find a comprehensive list of capabilities in the project-documentation. While the README evolves, a demo end-to-end example using Spring Boot has been created as an early preview for the community, and is summarized below.

Consumer-Provider Example

We use a simplified example of a Java ‘consumer’ which makes HTTP calls to a Payment Service (provider) where GET, POST, PUT and DELETE have been implemented. The 'provider' implements CRUD for the [Payment.java](https://github.com/intuit/karate/blob/develop/karate-demo/src/test/java/mock/contract/Payment.java) 'POJO', and the POST (or create) results in a message ([Shipment.java](https://github.com/intuit/karate/blob/develop/karate-demo/src/test/java/mock/contract/Shipment.java) as JSON) being placed on a queue, which the consumer is listening to.

ActiveMQ is being used for the sake of mixing an asynchronous flow into this example, and with the help of some simple utilities, we are able to mix asynchronous messaging into a Karate test as well as the test-double.

Server-Side Karate

Karate on the ‘other side of the fence’ (handling HTTP requests instead of making them) — turns out to be remarkably effective, yet simple.

  • ‘Native’ support for expressing JSON and XML
  • Manipulate or even transform payloads
  • Validate payloads, using a simpler alternative to JSON schema if needed
  • Karate is all about making HTTP calls, giving you the flexibility to call ‘downstream’ services if needed
  • In-memory JSON and JsonPath solves for ‘state’ and filtering if needed
  • Mix custom JavaScript (or even Java code) if needed — for complex logic
  • Easily ‘seed’ data or switch environment / config on start

If you think about it, all the above are sufficient to implement any micro-service. Karate’s DSL syntax is focused on exactly these aspects, thus opening up interesting possibilities.

It may be hard to believe that you can spin-up a ‘usable’ micro-service in minutes with Karate — but do try it and let me know what you think !