Add support for port and random port in WireMock

User can @AutoConfigureWireMock(port=0) for instance. Still missing:
declarative HTTPS.
This commit is contained in:
Dave Syer
2016-07-21 17:16:16 +01:00
parent 472cd89768
commit b47ea3b0cd
10 changed files with 251 additions and 9 deletions

View File

@@ -8,7 +8,7 @@ Olga Maciaszek-Sharma, Mariusz Smykuła, Dave Syer_
== Spring Cloud Contract
What you always need it confidence in pushing new features into a new application or service in a distributed system.
What you always need is confidence in pushing new features into a new application or service in a distributed system.
This project provides support for Consumer Driven Contracts and service schemas in Spring applications, covering a
range of options for writing tests, publishing them as assets, asserting that a contract is kept by producers
and consumers, for HTTP and message-based interactions.

View File

@@ -1,4 +1,88 @@
Modules giving you the possibility to use http://wiremock.org[WireMock] with different servers. Check out the
https://github.com/spring-cloud/spring-cloud-contract/tree/master/samples[samples] for more information.
Modules giving you the possibility to use
http://wiremock.org[WireMock] with different servers by using the
"ambient" server embedded in a Spring Boot application. Check out the
https://github.com/spring-cloud/spring-cloud-contract/tree/master/samples[samples]
for more details.
Currently we support Jetty, Native WireMock server, Tomcat and Undertow.
If you have a Spring Boot application that uses Tomcat as an embedded
server, for example (the default with `spring-boot-starter-web`), then
you can simply add `spring-cloud-contract-wiremock` to your classpath
and add `@AutoConfigureWireMock` in order to be able to use Wiremock
in your tests. Wiremock runs as a stub server and you can register
stub behaviour using a Java API or via static JSON declarations as
part of your test. Here's a simple example:
[source,java,indent=0]
----
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureWireMock
public class WiremockImportApplicationTests {
// A service that calls out over HTTP to localhost:8080
@Autowired
private Service service;
@Test
public void contextLoads() throws Exception {
// Using the WireMock APIs in the normal way:
stubFor(get(urlEqualTo("/resource"))
.willReturn(aResponse().withHeader("Content-Type", "text/plain").withBody("Hello World!")));
assertThat(this.service.go()).isEqualTo("Hello World!");
}
}
----
To start the stub server on a different port use `@AutoConfigureWireMock(port=9999)` (for example), and for a random port use the value 0. The stub server port will be bindable in the test application context as "wiremock.server.port". Using `@AutoConfigureWireMock` adds a bean of type `WiremockConfiguration` to your test application context, where it will be cached in between methods and classes having the same context, just like for normal Spring integration tests.
For a more conventional WireMock experience, using JUnit `@Rules` to
start and stop the server, just use the `WireMockSpring` convenience
class to obtain an `Options` instance:
[source,java,indent=0]
----
@ClassRule
public static WireMockClassRule wiremock = new WireMockClassRule(
WireMockSpring.options());
----
The use `@ClassRule` means that the server will shut down after all the methods in this class.
== WireMock and Spring MVC Mocks
Spring Cloud Contract provides a convenience class that can load JSON WireMock stubs into a Spring `MockRestServiceServer`. Here's an example:
[source,java,indent=0]
----
@Autowired
private RestTemplate restTemplate;
@Autowired
private Service service;
@Test
public void contextLoads() throws Exception {
MockRestServiceServer server = WireMockExpectations.with(this.restTemplate)
.baseUrl("http://example.org")
.expect("resource");
assertThat(this.service.go()).isEqualTo("Hello World");
server.verify();
}
----
The `baseUrl` is prepended to all mock calls, and the `expect()`
method takes a stub name as an argument, where the stubs are stored in
the classpath at `/stubs/<name>.json` by default. So in this example
the stub defined at `/stubs/resource.json` is loaded into the mock
server, so if the `RestTemplate` is asked to visit
`http://example.org/` it will get the responses as declared there. The
JSON format is the normal WireMock format which you can read about in
the WireMock website.
Currently we support Tomcat, Jetty and Undertow as Spring Boot
embedded servers, and Wiremock itself has "native" support for a
particular version of Jetty (currently 9.2). To use the native Jetty
you need to add the native wiremock dependencies and exclude the
Spring Boot container if there is one.