Move restdocs stuff into its own package

This commit is contained in:
Dave Syer
2016-07-26 14:10:09 +01:00
parent eb691717a2
commit 997a17f923
8 changed files with 43 additions and 11 deletions

View File

@@ -23,6 +23,32 @@ include::{doc_samples}/src/test/java/com/example/WiremockForDocsTests.java[tags=
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.
=== Registering Stubs Automatically
If you add a `stubs` attribute to your `@AutoConfigureWireMock` then
it will register WireMock JSON stubs from the file system or
classpath. The stubs attribute can be a resource pattern (ant-style)
or a directory, in which case `**/*.json` is appended. Example:
----
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureWireMock(stubs="classpath:/stubs")
public class WiremockImportApplicationTests {
@Autowired
private Service service;
@Test
public void contextLoads() throws Exception {
assertThat(this.service.go()).isEqualTo("Hello World!");
}
}
----
=== Alternative: Using JUnit Rules
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:

View File

@@ -1,6 +1,6 @@
package com.example.fraud;
import static org.springframework.cloud.contract.wiremock.RestDocsContracts.verify;
import static org.springframework.cloud.contract.wiremock.restdocs.RestDocsContracts.verify;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import java.math.BigDecimal;

View File

@@ -29,11 +29,15 @@ import org.springframework.core.env.MutablePropertySources;
import org.springframework.util.SocketUtils;
/**
* Listener that prepares the environment so that WireMock will work when it is
* initialized. For example, by finding free ports for the server to listen on.
*
* @author Dave Syer
*
*/
@Order(Ordered.LOWEST_PRECEDENCE)
public class WireMockApplicationListener implements ApplicationListener<ApplicationPreparedEvent> {
public class WireMockApplicationListener
implements ApplicationListener<ApplicationPreparedEvent> {
@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
@@ -49,9 +53,11 @@ public class WireMockApplicationListener implements ApplicationListener<Applicat
}
Map<String, Object> source = ((MapPropertySource) propertySources
.get("wiremock")).getSource();
source.put("wiremock.server.port", SocketUtils.findAvailableTcpPort(10000, 12500));
source.put("wiremock.server.port",
SocketUtils.findAvailableTcpPort(10000, 12500));
}
if (environment.getProperty("wiremock.server.https-port", Integer.class, 0) == 0) {
if (environment.getProperty("wiremock.server.https-port", Integer.class,
0) == 0) {
MutablePropertySources propertySources = environment.getPropertySources();
if (!propertySources.contains("wiremock")) {
propertySources.addFirst(
@@ -59,9 +65,9 @@ public class WireMockApplicationListener implements ApplicationListener<Applicat
}
Map<String, Object> source = ((MapPropertySource) propertySources
.get("wiremock")).getSource();
source.put("wiremock.server.https-port", SocketUtils.findAvailableTcpPort(12500, 15000));
source.put("wiremock.server.https-port",
SocketUtils.findAvailableTcpPort(12500, 15000));
}
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.cloud.contract.wiremock;
package org.springframework.cloud.contract.wiremock.restdocs;
import static org.assertj.core.api.Assertions.assertThat;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.cloud.contract.wiremock;
package org.springframework.cloud.contract.wiremock.restdocs;
/**
* @author Dave Syer

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.cloud.contract.wiremock;
package org.springframework.cloud.contract.wiremock.restdocs;
import org.springframework.boot.test.autoconfigure.restdocs.RestDocsMockMvcConfigurationCustomizer;
import org.springframework.context.annotation.Configuration;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.cloud.contract.wiremock;
package org.springframework.cloud.contract.wiremock.restdocs;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.delete;

View File

@@ -4,4 +4,4 @@ org.springframework.cloud.contract.wiremock.WireMockApplicationListener
# RestDocs Auto Configuration
org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs=\
org.springframework.cloud.contract.wiremock.WireMockRestDocsConfiguration
org.springframework.cloud.contract.wiremock.restdocs.WireMockRestDocsConfiguration