Webtestclient support (#757)
* Fix incorrect spring cloud dependencies versioning. * Test fix and minor refactoring. * Add docs. * Fixes after code review. fixes gh-422
This commit is contained in:
committed by
Marcin Grzejszczak
parent
fea5c743ce
commit
cf21f18938
@@ -180,8 +180,7 @@ include::{wiremock_tests}/src/test/java/org/springframework/cloud/contract/wirem
|
||||
|
||||
https://projects.spring.io/spring-restdocs[Spring REST Docs] can be used to generate
|
||||
documentation (for example in Asciidoctor format) for an HTTP API with Spring MockMvc
|
||||
or `WebTestClient` or
|
||||
Rest Assured. At the same time that you generate documentation for your API, you can also
|
||||
or `WebTestClient` or Rest Assured. At the same time that you generate documentation for your API, you can also
|
||||
generate WireMock stubs by using Spring Cloud Contract WireMock. To do so, write your
|
||||
normal REST Docs test cases and use `@AutoConfigureRestDocs` to have stubs be
|
||||
automatically generated in the REST Docs output directory. The following code shows an
|
||||
|
||||
@@ -363,10 +363,38 @@ public void validate_shouldMarkClientAsFraud() throws Exception {
|
||||
----
|
||||
|
||||
The preceding example uses Spring's `MockMvc` to run the tests. This is the default test
|
||||
mode for HTTP contracts. However, JAX-RX client and explicit HTTP invocations can also be
|
||||
mode for HTTP contracts. However, JAX-RS client and explicit HTTP invocations can also be
|
||||
used. (To do so, change the `testMode` property of the plugin to `JAX-RS` or `EXPLICIT`,
|
||||
respectively.)
|
||||
|
||||
Since 2.1.0, it is also possible to use `RestAssuredWebTestClient`with Spring's reactive `WebTestClient`
|
||||
run under the hood. This is particularly recommended while working with Reactive, `Web-Flux`-based applications.
|
||||
In order to use `WebTestClient` set `testMode` to `WEBTESTCLIENT`.
|
||||
|
||||
Here is an example of a test generated in `WEBTESTCLIENT` test mode:
|
||||
|
||||
[source,java,indent=0]
|
||||
----
|
||||
@Test
|
||||
public void validate_shouldRejectABeerIfTooYoung() throws Exception {
|
||||
// given:
|
||||
WebTestClientRequestSpecification request = given()
|
||||
.header("Content-Type", "application/json")
|
||||
.body("{\"age\":10}");
|
||||
|
||||
// when:
|
||||
WebTestClientResponse response = given().spec(request)
|
||||
.post("/check");
|
||||
|
||||
// then:
|
||||
assertThat(response.statusCode()).isEqualTo(200);
|
||||
assertThat(response.header("Content-Type")).matches("application/json.*");
|
||||
// and:
|
||||
DocumentContext parsedJson = JsonPath.parse(response.getBody().asString());
|
||||
assertThatJson(parsedJson).field("['status']").isEqualTo("NOT_OK");
|
||||
}
|
||||
----
|
||||
|
||||
Apart from the default JUnit 4, you can instead use JUnit 5 or Spock tests, by setting the plugin
|
||||
`testFramework` property to either `JUNIT5` or `Spock`.
|
||||
|
||||
|
||||
@@ -217,7 +217,7 @@ contracts {
|
||||
==== Configuration Options
|
||||
|
||||
* *testMode*: Defines the mode for acceptance tests. By default, the mode is MockMvc,
|
||||
which is based on Spring's MockMvc. It can also be changed to *JaxRsClient* or to
|
||||
which is based on Spring's MockMvc. It can also be changed to *WebTestClient*, *JaxRsClient* or to
|
||||
*Explicit* for real HTTP calls.
|
||||
* *imports*: Creates an array with imports that should be included in generated tests
|
||||
(for example ['org.myorg.Matchers']). By default, it creates an empty array.
|
||||
@@ -569,7 +569,7 @@ definition or the `execution` definition, as shown here:
|
||||
==== Configuration Options
|
||||
|
||||
* *testMode*: Defines the mode for acceptance tests. By default, the mode is MockMvc,
|
||||
which is based on Spring's MockMvc. It can also be changed to *JaxRsClient* or to
|
||||
which is based on Spring's MockMvc. It can also be changed to *WebTestClient*, *JaxRsClient* or to
|
||||
*Explicit* for real HTTP calls.
|
||||
* *basePackageForTests*: Specifies the base package for all generated tests. If not set,
|
||||
the value is picked from `baseClassForTests`'s package and from `packageWithBaseClasses`.
|
||||
|
||||
Reference in New Issue
Block a user