web dependency is optional, StubRunner listener conditional

without this change we always include `spring-boot-starter-web` to Stub Runner. With this change the dependency is optional. Thanks to this reactive applications will be able to work fine with contract on the classpath.

without this change we always run Stub Runner test execution listener, even if the test class is not annotated. With this change we run the listener only, if the test class was annotated with `@AutoConfigureStubRunner`.

Also we add information about the `java.net.SocketException: Unexpected end of file from server` exception to the docs and how to override that issue.

Fixes gh-798, gh-809, gh-799
This commit is contained in:
Marcin Grzejszczak
2018-12-16 13:36:40 +01:00
parent 34f7cd31ed
commit 51f7ef6f67
12 changed files with 317 additions and 52 deletions

View File

@@ -3,6 +3,7 @@ image::https://badges.gitter.im/Join%20Chat.svg[Gitter, link="https://gitter.im/
image::https://codecov.io/gh/spring-cloud/spring-cloud-contract/branch/{branch}/graph/badge.svg["codecov", link="https://codecov.io/gh/spring-cloud/spring-cloud-contract"]
image::https://circleci.com/gh/spring-cloud/spring-cloud-contract.svg?style=svg["CircleCI", link="https://circleci.com/gh/spring-cloud/spring-cloud-contract"]
:introduction_url: ../../../..
:verifier_core_path: {introduction_url}/spring-cloud-contract-verifier
== Spring Cloud Contract

View File

@@ -936,4 +936,40 @@ started will be attached.
Yes! With version 1.2.0 we've added such a possibility. It's enough to call `file(...)` method in the
DSL and provide a path relative to where the contract lays.
If you're using YAML just use the `bodyFromFile` property.
If you're using YAML just use the `bodyFromFile` property.
==== Why do I sometimes get `SocketException`
When using an HTTP client (e.g. `RestTemplate`) and running several tests that share a Spring context, you might sometimes get the following exception:
```
java.net.SocketException: Unexpected end of file from server
```
Tom Akehurst, the creator of WireMock did the following analysis of this issue.
> I looked at tcpdump while running the failing test. `HttpUrlConnection` is doing something weird - it's creating a connection in a previous test case, which works fine, then the usual `fin` -> `fin ack` etc. ending handshake happens. But it seems it isn't discarded, but reused after that. Because the server thinks (rightly) that the connection is closed, it just sends a RST packet. Calling the `/__admin` endpoint just happened to remove the dead connection from the pool. This also fixes the problem (which using the Java HTTP client): System.setProperty("http.keepAlive", "false");
There are the ways to solve this problem.
First, just use a different HTTP client for `RestTemplate`. Example for using Apache HTTP client:
.pom.xml
[source,xml,indent=0]
----
include::{standalone_restdocs_path}/http-client/pom.xml[tags=httpclient,indent=0]
----
[source,java,indent=0]
----
include::{standalone_restdocs_path}/http-client/src/main/java/com/example/loan/LoanApplicationService.java[tags=custom_request_factory,indent=0]
----
Second option is to set the system property. You can set it either in code or pass it to your tests via a plugin.
[source,java,indent=0]
----
static {
System.setProperty("http.keepAlive", "false");
}
----