DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Using the OpenAPI Generator To Build Out an Integration Test Client
  • Write MUnit to Validate API Integration (Mule 4)
  • User-Friendly API Publishing and Testing With Retrofit
  • How to Introduce a New API Quickly Using Micronaut

Trending

  • How to Use AWS Aurora Database for a Retail Point of Sale (POS) Transaction System
  • Caching 101: Theory, Algorithms, Tools, and Best Practices
  • How to Introduce a New API Quickly Using Micronaut
  • Data Lake vs. Warehouse vs. Lakehouse vs. Mart: Choosing the Right Architecture for Your Business
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. API Consumer-Contract Tests and Test Doubles With Karate

API Consumer-Contract Tests and Test Doubles With Karate

Learn about Karate, a project enabling testers to spin up test doubles to mock a web service API dependency with its embedded JavaScript engine.

By 
Peter Thomas user avatar
Peter Thomas
·
Jan. 16, 18 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
16.8K Views

Join the DZone community and get the full member experience.

Join For Free

Karate recently crossed 500 stars on GitHub and has been very well-received, even starting 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 unavailable 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 “misuse” 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.

Image title

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 POJO, and the POST (or create) results in a message (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.

Image title

Ref Source Code Description
C

Consumer.java

The 'consumer' or client application that consumes the demo 'Payment Service' and also listens to a queue

P

PaymentService.java

The provider 'Payment Service' implemented in Spring Boot and using an in-memory data-store

1

ConsumerIntegrationTest.java

An end-to-end integration test of the consumer that needs the real provider to be up and running

KC

payment-service.feature

A 'normal' Karate functional-test that tests the 'contract' of the Payment Service from the perspective of the consumer

2

PaymentServiceContractTest.java

JUnit runner for the above Karate 'contract' test, that depends on the real provider being up and running

KP

payment-service-mock.feature

A 'state-ful' mock (or stub) that fully implements the 'contract' ! Yes, really.

3

PaymentServiceContractUsingMockTest.java

Uses the above 'stub' to run the Payment Service 'contract' test

4

ConsumerUsingMockTest.java

Uses the 'fake' Payment Service 'stub' to run an integration test for the real consumer

KX

payment-service-proxy.feature

Karate can act as a proxy with 'gateway like' capabilities, you can choose to either stub a response or delegate to a remote provider, depending on the incoming request. Think of the 'X' as being able to transform the HTTP request and response payloads as they pass through (and before returning)

5a

ConsumerUsingProxyHttpTest.java

Here Karate is set up to act as an HTTP proxy, the advantage is that the consumer can use the 'real' provider URL, which simplifies configuration, provided that you can configure the consumer to use an HTTP proxy (ideally in a non-invasive fashion)

5b

ConsumerUsingProxyRewriteTest.java

Karate acts as a URL 're-writing' proxy. Here the consumer 'knows' only about the proxy. In this mode (as well as the above 'HTTP proxy' mode which uses the same script file), you can choose to either stub a response - or even forward the incoming HTTP request onto any remote URL you choose.

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 microservice. 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 microservice in minutes with Karate — but do try it and let me know what you think!

API integration test

Published at DZone with permission of Peter Thomas. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Using the OpenAPI Generator To Build Out an Integration Test Client
  • Write MUnit to Validate API Integration (Mule 4)
  • User-Friendly API Publishing and Testing With Retrofit
  • How to Introduce a New API Quickly Using Micronaut

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!