0% found this document useful (0 votes)
7 views11 pages

Microservices Patterns - Testing Microservices III

The document discusses testing microservices using Spring Cloud Contract, which allows for writing contracts that define interactions between consumers and providers. It outlines a collaborative process where consumer teams create contracts for API Gateway interactions with Order Service, which are then used by the provider team for testing. The contracts serve as both test data and specifications for expected behavior, facilitating testing without needing to run the actual services.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views11 pages

Microservices Patterns - Testing Microservices III

The document discusses testing microservices using Spring Cloud Contract, which allows for writing contracts that define interactions between consumers and providers. It outlines a collaborative process where consumer teams create contracts for API Gateway interactions with Order Service, which are then used by the provider team for testing. The contracts serve as both test data and specifications for expected behavior, facilitating testing without needing to run the actual services.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Microservices Patterns

Testing Microservices III


Testing Services Using Spring Cloud Contract

● It provides a Groovy domain-specific language (DSL) for writing contracts.

● Each contract is a concrete example of an interaction between a consumer and a


provider, such as an HTTP request and response.

● Spring Cloud Contract code generates contract tests for the provider.

● It also configures mocks, such as a mock HTTP server, for consumer integration
tests.
Spring Cloud Contract Example

● You’re working on API Gateway and want to write a consumer contract test for Order
Service.

● The following figure shows the process, which requires you to collaborate with Order
Service teams.

● You write contracts that define how API Gateway interacts with Order Service.

● The Order Service team uses these contracts to test Order Service, and you use them to
test API Gateway.
Spring Cloud Contract Example
Spring Cloud Contract Example

The sequence of steps is as follows:

● 1. You write one or more contracts, each contract consists of an HTTP request that API
Gateway might send to Order Service and an expected HTTP response. You give the
contracts, perhaps via a Git pull request, to the Order Service team.

● 2. The Order Service team tests Order Service using consumer contract tests, which Spring
Cloud Contract code generates from contracts.

● 3. The Order Service team publishes the contracts that tested Order Service to a Maven
repository.

● 4. You use the published contracts to write tests for API Gateway.
Spring Cloud Contract Example

● The contracts are the key part of this testing strategy. The following code shows an
example Spring Cloud Contract. It consists of an HTTP request and an HTTP response.

● The request element is an HTTP request for the REST endpoint GET /orders/ {orderId}.

● The response element is an HTTP response that describes an Order expected by API
Gateway.

● The Groovy contracts are part of the provider’s code base.


Spring Cloud Contract Example
Spring Cloud Contract Example

● Each consumer team writes contracts that describe how their service interacts with the
provider and gives them, perhaps via a Git pull request, to the provider team.

● The provider team is responsible for packaging the contracts as a JAR and publishing them
to a Maven repository.

● The consumer-side tests download the JAR from the repository.

● Each contract’s request and response play dual roles of test data and the specification of
expected behavior.
Spring Cloud Contract Example

● In a consumer-side test, the contract is used to configure a stub, which is similar to a


Mockito mock object and simulates the behavior of Order Service.

● It enables API Gateway to be tested without running Order Service.

● In the provider-side test, the generated test class invokes the provider with the contract’s
request and verifies that it returns a response that matches the contract’s response.
The Next Post

Writing Unit Tests for a Service

You might also like