You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: spring-cloud-contract.adoc
+19-10Lines changed: 19 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,10 +12,10 @@ It's always tempting to set up an environment for end-to-end tests, spawn all ap
12
12
13
13
A potential solution to this problem are Contract Tests. Before we go into details of what those are, let's define some terms:
14
14
15
-
*producer*: The server-side owner (for example, the owner of a HTTP API) or a producer of a message sent through a queue, such as RabbitMQ.
16
-
*consumer*: The application that consumes the HTTP API or listens to messages received through (for example) RabbitMQ.
17
-
*contract*: An agreement between the producer and the consumer on what the communication should look like. **It is not a schema**. It is more of a scenario of usage. For example, for this particular scenario, I expect a specified input and then I reply with a specified output.
18
-
*contract test*: A test that verifies that the producer and the consumer can integrate with each other. **It does not mean that the functionality works**. This distinction is important, because you would not want to duplicate your work by writing a contract for each feature. Contract tests assert that the integration between the producer and the consumer meets the requirements defined in the contract. Their main advantage is that they are fast and reliable.
15
+
* *producer*: The server-side owner (for example, the owner of a HTTP API) or a producer of a message sent through a queue, such as RabbitMQ.
16
+
* *consumer*: The application that consumes the HTTP API or listens to messages received through (for example) RabbitMQ.
17
+
* *contract*: An agreement between the producer and the consumer on what the communication should look like. **It is not a schema**. It is more of a scenario of usage. For example, for this particular scenario, I expect a specified input and then I reply with a specified output.
18
+
* *contract test*: A test that verifies that the producer and the consumer can integrate with each other. **It does not mean that the functionality works**. This distinction is important, because you would not want to duplicate your work by writing a contract for each feature. Contract tests assert that the integration between the producer and the consumer meets the requirements defined in the contract. Their main advantage is that they are fast and reliable.
19
19
20
20
The following example shows a contract written in YAML:
21
21
@@ -76,8 +76,8 @@ response: # (7)
76
76
77
77
This article focuses on two main types of contract tests: Producer Contract testing and Consumer-Driven Contract testing. The main difference between them is the cooperation style of the producer and the consumer.
78
78
79
-
* In the Producer Contract testing approach, the producer defines the contracts and writes the contract tests, describes the API, and publishes the stubs without any cooperation with its clients. Often, this happens when the API is public and the owners of the API don't even know who exactly is using it. An example is https://start.spring.io[Spring Initializr], which publishes its stubs with https://cloud.spring.io/spring-cloud-static/Edgware.SR2/multi/multi__spring_cloud_contract_wiremock.html#_generating_stubs_using_rest_docs[Spring Rest Docs] tests. The stubs for version `0.5.0.BUILD-SNAPSHOT` are available https://repo.spring.io/libs-snapshot/io/spring/initializr/initializr-web/0.5.0.BUILD-SNAPSHOT/[here with the `stubs` classifier].
80
-
* In the Consumer-Driven Contract testing approach, the contracts are suggested by the consumers, in strong cooperation with the producer. The producer knows exactly which consumer defined which contract and which one gets broken when the contract compatibility gets broken. This approach is more common when working with an internal API.
79
+
* In the *Producer Contract* testing approach, the producer defines the contracts and writes the contract tests, describes the API, and publishes the stubs without any cooperation with its clients. Often, this happens when the API is public and the owners of the API don't even know who exactly is using it. An example is https://start.spring.io[Spring Initializr], which publishes its stubs with https://cloud.spring.io/spring-cloud-static/Edgware.SR2/multi/multi__spring_cloud_contract_wiremock.html#_generating_stubs_using_rest_docs[Spring Rest Docs] tests. The stubs for version `0.5.0.BUILD-SNAPSHOT` are available https://repo.spring.io/libs-snapshot/io/spring/initializr/initializr-web/0.5.0.BUILD-SNAPSHOT/[here with the `stubs` classifier].
80
+
* In the *Consumer-Driven Contract* testing approach, the contracts are suggested by the consumers, in strong cooperation with the producer. The producer knows exactly which consumer defined which contract and which one gets broken when the contract compatibility gets broken. This approach is more common when working with an internal API.
81
81
82
82
In both cases, the contracts can be defined in the repository of the producer (either defined with a DSL or by writing contract tests) or an external repo where all the contracts are stored.
0 commit comments