diff --git a/docs/src/main/asciidoc/spring-cloud-wiremock.adoc b/docs/src/main/asciidoc/spring-cloud-wiremock.adoc index 0d67c801ee..a729de620f 100644 --- a/docs/src/main/asciidoc/spring-cloud-wiremock.adoc +++ b/docs/src/main/asciidoc/spring-cloud-wiremock.adoc @@ -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/.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 diff --git a/samples/wiremock-jetty/src/test/java/com/example/WiremockMockServerApplicationTests.java b/samples/wiremock-jetty/src/test/java/com/example/WiremockMockServerApplicationTests.java index 9c37cf924e..100876325e 100644 --- a/samples/wiremock-jetty/src/test/java/com/example/WiremockMockServerApplicationTests.java +++ b/samples/wiremock-jetty/src/test/java/com/example/WiremockMockServerApplicationTests.java @@ -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(); } diff --git a/samples/wiremock-tomcat/src/test/java/com/example/WiremockMockServerApplicationTests.java b/samples/wiremock-tomcat/src/test/java/com/example/WiremockMockServerApplicationTests.java index 9c37cf924e..33f609b71b 100644 --- a/samples/wiremock-tomcat/src/test/java/com/example/WiremockMockServerApplicationTests.java +++ b/samples/wiremock-tomcat/src/test/java/com/example/WiremockMockServerApplicationTests.java @@ -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(); } diff --git a/samples/wiremock-undertow/src/test/java/com/example/WiremockMockServerApplicationTests.java b/samples/wiremock-undertow/src/test/java/com/example/WiremockMockServerApplicationTests.java index 9c37cf924e..fef1ace431 100644 --- a/samples/wiremock-undertow/src/test/java/com/example/WiremockMockServerApplicationTests.java +++ b/samples/wiremock-undertow/src/test/java/com/example/WiremockMockServerApplicationTests.java @@ -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(); } diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockExpectations.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockRestServiceServer.java similarity index 75% rename from spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockExpectations.java rename to spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockRestServiceServer.java index d764ed5aba..cda673402e 100644 --- a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockExpectations.java +++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockRestServiceServer.java @@ -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); } } diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/WiremockMockServerApplicationTests.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/WiremockMockServerApplicationTests.java index d22632d07c..d1535f5f2e 100644 --- a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/WiremockMockServerApplicationTests.java +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/WiremockMockServerApplicationTests.java @@ -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(); }