diff --git a/multi/multi__contract_dsl.html b/multi/multi__contract_dsl.html index a488f4b6e2..9cab6299d4 100644 --- a/multi/multi__contract_dsl.html +++ b/multi/multi__contract_dsl.html @@ -1,6 +1,6 @@
-![]() | Important | |||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Remember that, inside the contract file, you have to provide the fully +
If you want to ignore a contract, you can either set a value of ignored contracts in the
plugin configuration or set the org.springframework.cloud.contract.spec.Contract.make {
ignored()
-}Starting with version Starting with version └── src
└── test
└── resources
@@ -105,7 +105,7 @@ Contract.make {
}Further assume that the JSON files is as follows: request.json { "status" : "REQUEST" } response.json { "status" : "RESPONSE" } When test or stub generation takes place, the contents of the file is passed to the body
of a request or a response. That works because of the The following methods can be called in the top-level closure of a contract definition. +lays. The following methods can be called in the top-level closure of a contract definition.
org.springframework.cloud.contract.spec.Contract.make {
// Definition of HTTP request part of the contract
// (this can be a valid request or invalid depending
@@ -125,7 +125,7 @@ lays.// Contract priority, which can be used for overriding // contracts (1 is highest). Priority is optional. priority 1 -}The HTTP protocol requires only method and address to be specified in a request. The same information is mandatory in request definition of the Contract. org.springframework.cloud.contract.spec.Contract.make {
request {
// HTTP request method (GET/POST/PUT/DELETE).
@@ -283,7 +283,7 @@ such as The response must contain an HTTP status code and may contain other information. The + ''' The response must contain an HTTP status code and may contain other information. The following code shows an example: org.springframework.cloud.contract.spec.Contract.make {
request {
//...
@@ -294,11 +294,11 @@ following code shows an example:The contract can contain some dynamic properties: timestamps, IDs, and so on. You do not +specified the same way as in the request (see the previous paragraph). The contract can contain some dynamic properties: timestamps, IDs, and so on. You do not
want to force the consumers to stub their clocks to always return the same value of time
so that it gets matched by the stub. You can provide the dynamic parts in your contracts
in two ways: pass them directly in the body or set them in separate sections called
- You can set the properties inside the body either with the You can set the properties inside the body either with the value(consumer(...), producer(...)) value(c(...), p(...)) @@ -307,7 +307,7 @@ value(client(...), server(...)) The following example shows how to set d $(c(...), p(...)) $(stub(...), test(...)) $(client(...), server(...)) Both approaches work equally well. You can use regular expressions to write your requests in Contract DSL. Doing so is +method. Subsequent sections take a closer look at what you can do with those values. You can use regular expressions to write your requests in Contract DSL. Doing so is particularly useful when you want to indicate that a given response should be provided for requests that follow a given pattern. Also, you can use regular expressions when you need to use patterns and not exact values both for your test and your server side tests. The following example shows how to use regular expressions to write a request: org.springframework.cloud.contract.spec.Contract.make {
@@ -453,7 +453,7 @@ String nonBlank() {
message: "User not found by email = [${value(producer(regex(email())), consumer('not.existing@user.com'))}]"
)
}
-}It is possible to provide optional parameters in your contract. However, you can provide +} It is possible to provide optional parameters in your contract. However, you can provide optional parameters only for the following:
The following example shows how to provide optional parameters: org.springframework.cloud.contract.spec.Contract.make {
priority 1
request {
@@ -518,7 +518,7 @@ expression that must be present 0 or more times.You can define a method call that executes on the server side during the test. Such a +''' You can define a method call that executes on the server side during the test. Such a method can be added to the class defined as "baseClassForTests" in the configuration. The following code shows an example of the contract portion of the test case: org.springframework.cloud.contract.spec.Contract.make {
request {
@@ -581,7 +581,7 @@ It should resemble the following code:The best situation is to provide fixed values, but sometimes you need to reference a + assertThat(response.statusCode()).isEqualTo(200); The best situation is to provide fixed values, but sometimes you need to reference a
request in your response. To do so, you can use the
|
escapejsonbody: Escapes the request body in a format that can be embedded in a JSON.jsonpath: For a given parameter, find an object in the request body.WireMock lets you register custom extensions. By default, Spring Cloud Contract registers
the transformer, which lets you reference a request from a response. If you want to
provide your own extensions, you can register an implementation of the
org.springframework.cloud.contract.verifier.dsl.wiremock.WireMockExtensions interface.
@@ -723,7 +723,7 @@ org.springframework.cloud.contract.stubrunner.provider.wiremock.TestWireMockExte
}
}
![]() | Important |
|---|---|
Remember to override the |
If you work with Pact, the following discussion may seem familiar. +want the transformation to be applied only for a mapping that explicitly requires it.
If you work with Pact, the following discussion may seem familiar. Quite a few users are used to having a separation between the body and setting the dynamic parts of a contract.
You can use two separate sections:
stubMatchers, which lets you define the dynamic values that should end up in a stub.
You can set it in the request or inputMessage part of your contract.testMatchers, which is present in the response or outputMessage side of the
@@ -994,7 +994,7 @@ and:
assertThat(parsedJson.read("\$.events[0].eventId", String.class)).matches("^([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})\$")
assertThat(parsedJson.read("\$.events[0].status", String.class)).matches(".+")As you can see, the assertion is malformed. Only the first element of the array got
asserted. In order to fix this, you should apply the assertion to the whole $.events
-collection and assert it with the byCommand(…) method.
The Spring Cloud Contract Verifier supports the JAX-RS 2 Client API. The base class needs
+collection and assert it with the byCommand(…) method.
The Spring Cloud Contract Verifier supports the JAX-RS 2 Client API. The base class needs
to define protected WebTarget webTarget and server initialization. The only option for
testing JAX-RS API is to start a web server. Also, a request with a body needs to have a
content type set. Otherwise, the default of application/octet-stream gets used.
In order to use JAX-RS mode, use the following settings:
testMode == 'JAXRSCLIENT'The following example shows a generated test API:
'''
@@ -1019,7 +1019,7 @@ content type set. Otherwise, the default of application/oc
// and:
DocumentContext parsedJson = JsonPath.parse(responseAsString);
assertThatJson(parsedJson).field("['property1']").isEqualTo("a");
-'''If you’re using asynchronous communication on the server side (your controllers are +'''
If you’re using asynchronous communication on the server side (your controllers are
returning Callable, DeferredResult, and so on), then, inside your contract, you must
provide a sync() method in the response section. The following code shows an example:
org.springframework.cloud.contract.spec.Contract.make {
request {
@@ -1031,7 +1031,7 @@ provide a sync() method in the 'Passed'
async()
}
-}Spring Cloud Contract supports context paths.
![]() | Important | |||||
|---|---|---|---|---|---|---|
The only change needed to fully support context paths is the switch on the +} Spring Cloud Contract supports context paths.
Maven. @@ -1075,8 +1075,8 @@ socket. Consider the following contract: or } } If you do it this way:
The DSL for messaging looks a little bit different than the one that focuses on HTTP. The -following sections explain the differences: The DSL for messaging looks a little bit different than the one that focuses on HTTP. The +following sections explain the differences: The output message can be triggered by calling a method (such as a def dsl = Contract.make {
// Human readable description
description 'Some description'
@@ -1101,7 +1101,7 @@ started and a message was sent), as shown in the following example:The output message can be triggered by receiving a message, as shown in the following
+the The output message can be triggered by receiving a message, as shown in the following example: def dsl = Contract.make {
description 'Some Description'
label 'some_label'
@@ -1131,7 +1131,7 @@ example:In HTTP, you have a notion of In HTTP, you have a notion of Contract.make {
bookName: 'foo'
])
}
-}You can define multiple contracts in one file. Such a contract might resemble the +in the genertaed test. You can define multiple contracts in one file. Such a contract might resemble the following example: import org.springframework.cloud.contract.spec.Contract [ @@ -1227,4 +1227,4 @@ index of the contract in the list. As you can see, the first file got the
|