Rename expect() to stubs() in mock server config

This commit is contained in:
Dave Syer
2016-07-26 14:28:16 +01:00
parent 997a17f923
commit aa1d7dc074
6 changed files with 41 additions and 38 deletions

View File

@@ -71,14 +71,16 @@ Spring `MockRestServiceServer`. Here's an example:
include::{doc_samples}/src/test/java/com/example/WiremockForDocsMockServerApplicationTests.java[tags=wiremock_test]
----
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.
The `baseUrl` is prepended to all mock calls, and the `stubs()`
method takes a stub path resource pattern as an argument. 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. More than one stub pattern can be specified, and each one can
be a directory (for a recursive list of all ".json"), or a fixed
filename (like in the example above) or an ant-style pattern. 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

View File

@@ -7,7 +7,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.cloud.contract.wiremock.WireMockExpectations;
import org.springframework.cloud.contract.wiremock.WireMockRestServiceServer;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.client.MockRestServiceServer;
@@ -26,9 +26,9 @@ public class WiremockMockServerApplicationTests {
@Test
public void contextLoads() throws Exception {
MockRestServiceServer server = WireMockExpectations.with(this.restTemplate) //
MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //
.baseUrl("http://example.org") //
.expect("resource");
.stubs("classpath:/stubs/resource.json");
assertThat(this.service.go()).isEqualTo("Hello World");
server.verify();
}

View File

@@ -7,7 +7,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.cloud.contract.wiremock.WireMockExpectations;
import org.springframework.cloud.contract.wiremock.WireMockRestServiceServer;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.client.MockRestServiceServer;
@@ -26,9 +26,9 @@ public class WiremockMockServerApplicationTests {
@Test
public void contextLoads() throws Exception {
MockRestServiceServer server = WireMockExpectations.with(this.restTemplate) //
MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //
.baseUrl("http://example.org") //
.expect("resource");
.stubs("classpath:/stubs/**/*.json");
assertThat(this.service.go()).isEqualTo("Hello World");
server.verify();
}

View File

@@ -7,7 +7,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.cloud.contract.wiremock.WireMockExpectations;
import org.springframework.cloud.contract.wiremock.WireMockRestServiceServer;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.client.MockRestServiceServer;
@@ -26,9 +26,9 @@ public class WiremockMockServerApplicationTests {
@Test
public void contextLoads() throws Exception {
MockRestServiceServer server = WireMockExpectations.with(this.restTemplate) //
MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //
.baseUrl("http://example.org") //
.expect("resource");
.stubs("classpath:/stubs");
assertThat(this.service.go()).isEqualTo("Hello World");
server.verify();
}

View File

@@ -24,6 +24,7 @@ import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.http.MediaType;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import com.github.tomakehurst.wiremock.common.Json;
@@ -36,40 +37,40 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
* @author Dave Syer
*
*/
public class WireMockExpectations {
public class WireMockRestServiceServer {
private final PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
private String prefix = "classpath:/mappings/";
private String suffix = ".json";
private String baseUrl = "";
private MockRestServiceServer server;
private WireMockExpectations(RestTemplate restTemplate) {
private WireMockRestServiceServer(RestTemplate restTemplate) {
this.server = MockRestServiceServer.bindTo(restTemplate).build();
}
public WireMockExpectations baseUrl(String baseUrl) {
this.baseUrl = baseUrl;
return this;
}
public WireMockExpectations prefix(String prefix) {
this.prefix = prefix;
return this;
}
public WireMockExpectations suffix(String suffix) {
public WireMockRestServiceServer suffix(String suffix) {
this.suffix = suffix;
return this;
}
public MockRestServiceServer expect(String... locations) {
public WireMockRestServiceServer baseUrl(String baseUrl) {
this.baseUrl = baseUrl;
return this;
}
public MockRestServiceServer stubs(String... locations) {
for (String location : locations) {
try {
for (Resource resource : this.resolver.getResources(this.prefix + location + this.suffix)) {
if (!StringUtils.getFilename(location).contains(".") && !location.contains("*")) {
if (!location.endsWith("/")) {
location = location + "/";
}
location = location + "/**/*" + this.suffix;
}
for (Resource resource : this.resolver.getResources(location)) {
StubMapping mapping;
mapping = Json.read(StreamUtils.copyToString(resource.getInputStream(), Charset.defaultCharset()),
StubMapping.class);
@@ -84,8 +85,8 @@ public class WireMockExpectations {
return this.server;
}
public static WireMockExpectations with(RestTemplate restTemplate) {
return new WireMockExpectations(restTemplate);
public static WireMockRestServiceServer with(RestTemplate restTemplate) {
return new WireMockRestServiceServer(restTemplate);
}
}

View File

@@ -25,9 +25,9 @@ public class WiremockMockServerApplicationTests {
@Test
public void contextLoads() throws Exception {
MockRestServiceServer server = WireMockExpectations.with(this.restTemplate) //
MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //
.baseUrl("http://example.org") //
.expect("resource");
.stubs("classpath:/mappings");
assertThat(this.service.go()).isEqualTo("Hello World");
server.verify();
}