Added WireMockConfigurationCustomizer
with this change it's possible to customize the WireMockConfiguration set up by Spring Cloud Contract fixes gh-502
This commit is contained in:
@@ -160,6 +160,19 @@ Spring Boot embedded servers, and Wiremock itself has "native" support for a par
|
||||
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).
|
||||
|
||||
=== Customization of WireMock configuration
|
||||
|
||||
You can register a bean of `org.springframework.cloud.contract.wiremock.WireMockConfigurationCustomizer` type
|
||||
in order to customize the WireMock configuration (e.g. add custom transformers).
|
||||
Example:
|
||||
|
||||
[source,java,indent=0]
|
||||
----
|
||||
include::{wiremock_tests}/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockConfigurationCustomizerTests.java[tags=customizer_1]
|
||||
// perform your customization here
|
||||
include::{wiremock_tests}/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockConfigurationCustomizerTests.java[tags=customizer_2]
|
||||
----
|
||||
|
||||
=== Generating Stubs using REST Docs
|
||||
|
||||
https://projects.spring.io/spring-restdocs[Spring REST Docs] can be used to generate
|
||||
|
||||
@@ -62,6 +62,9 @@ public class WireMockConfiguration implements SmartLifecycle {
|
||||
@Autowired(required = false)
|
||||
private Options options;
|
||||
|
||||
@Autowired(required = false)
|
||||
private WireMockConfigurationCustomizer customizer;
|
||||
|
||||
@Autowired
|
||||
private DefaultListableBeanFactory beanFactory;
|
||||
|
||||
@@ -84,6 +87,9 @@ public class WireMockConfiguration implements SmartLifecycle {
|
||||
registerFiles(factory);
|
||||
factory.notifier(new Slf4jNotifier(true));
|
||||
this.options = factory;
|
||||
if (this.customizer != null) {
|
||||
this.customizer.customize(factory);
|
||||
}
|
||||
}
|
||||
this.server = new WireMockServer(this.options);
|
||||
registerStubs();
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.springframework.cloud.contract.wiremock;
|
||||
|
||||
/**
|
||||
* Allows customization of {@link com.github.tomakehurst.wiremock.core.WireMockConfiguration}
|
||||
*
|
||||
* @author Marcin Grzejszczak
|
||||
* @since 1.2.2
|
||||
*/
|
||||
public interface WireMockConfigurationCustomizer {
|
||||
|
||||
void customize(com.github.tomakehurst.wiremock.core.WireMockConfiguration config);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package org.springframework.cloud.contract.wiremock;
|
||||
|
||||
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
|
||||
import org.junit.Test;
|
||||
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.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes={WiremockTestsApplication.class,
|
||||
AutoConfigureWireMockConfigurationCustomizerTests.Config.class},
|
||||
properties="app.baseUrl=http://localhost:${wiremock.server.port}",
|
||||
webEnvironment=WebEnvironment.NONE)
|
||||
@DirtiesContext
|
||||
@AutoConfigureWireMock(port=0, stubs="file:src/test/resources/io.stubs/mappings")
|
||||
public class AutoConfigureWireMockConfigurationCustomizerTests {
|
||||
|
||||
@Autowired
|
||||
private Service service;
|
||||
@Autowired
|
||||
private Config config;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
assertThat(this.service.go()).isEqualTo("Hello World");
|
||||
assertThat(this.config.isExecuted()).isTrue();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class Config {
|
||||
|
||||
boolean executed = false;
|
||||
|
||||
// tag::customizer_1[]
|
||||
@Bean WireMockConfigurationCustomizer optionsCustomizer() {
|
||||
return new WireMockConfigurationCustomizer() {
|
||||
@Override public void customize(WireMockConfiguration options) {
|
||||
// end::customizer_1[]
|
||||
assertThat(options.portNumber()).isGreaterThan(0);
|
||||
Config.this.executed = true;
|
||||
// tag::customizer_2[]
|
||||
}
|
||||
};
|
||||
}
|
||||
// end::customizer_2[]
|
||||
|
||||
public boolean isExecuted() {
|
||||
return executed;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -57,4 +57,8 @@ class Service {
|
||||
public String go() {
|
||||
return this.restTemplate.getForEntity(this.base + "/test", String.class).getBody();
|
||||
}
|
||||
|
||||
public void setBase(String base) {
|
||||
this.base = base;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user