From 67119e62f6b30da56b06aade87ec3ba61de7fd24 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Wed, 11 Dec 2019 13:13:37 +0100 Subject: [PATCH] Adds a flag to disable stubs after each test excution; fixes gh-1286 --- .../asciidoc/_project-features-wiremock.adoc | 1 + .../wiremock/WireMockConfiguration.java | 22 ++++- .../WireMockTestExecutionListener.java | 18 +++- ...ithResetAfterEachTestApplicationTests.java | 89 +++++++++++++++++++ 4 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockWithResetAfterEachTestApplicationTests.java diff --git a/docs/src/main/asciidoc/_project-features-wiremock.adoc b/docs/src/main/asciidoc/_project-features-wiremock.adoc index c2813dbfc6..db4484d9cc 100644 --- a/docs/src/main/asciidoc/_project-features-wiremock.adoc +++ b/docs/src/main/asciidoc/_project-features-wiremock.adoc @@ -29,6 +29,7 @@ property. Using `@AutoConfigureWireMock` adds a bean of type `WiremockConfigurat your test application context, where it is cached between methods and classes having the same context. The same is true for Spring integration tests. Also, you can inject a bean of type `WireMockServer` into your test. +The registered WireMock server is reset after each test class, however, if you need to reset it after each test method, just set the `wiremock.reset-mappings-after-each-test` property to `true`. [[features-wiremock-registering-stubs]] === Registering Stubs Automatically diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockConfiguration.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockConfiguration.java index c466da4e90..63a07dbadb 100644 --- a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockConfiguration.java +++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockConfiguration.java @@ -109,7 +109,7 @@ public class WireMockConfiguration implements SmartLifecycle { this.customizer.customize(factory); } } - resetMappings(); + reRegisterServerWithResetMappings(); reRegisterBeans(); updateCurrentServer(); } @@ -146,14 +146,20 @@ public class WireMockConfiguration implements SmartLifecycle { } } - void resetMappings() { + void reRegisterServerWithResetMappings() { reRegisterServer(); + if (this.server.isRunning()) { + resetMappings(); + updateCurrentServer(); + } + } + + void resetMappings() { if (this.server.isRunning()) { this.server.resetAll(); WireMock.reset(); registerStubs(); logRegisteredMappings(); - updateCurrentServer(); } } @@ -282,6 +288,8 @@ class WireMockProperties { private boolean restTemplateSslEnabled; + private boolean resetMappingsAfterEachTest; + public boolean isRestTemplateSslEnabled() { return this.restTemplateSslEnabled; } @@ -290,6 +298,14 @@ class WireMockProperties { this.restTemplateSslEnabled = restTemplateSslEnabled; } + public boolean isResetMappingsAfterEachTest() { + return this.resetMappingsAfterEachTest; + } + + public void setResetMappingsAfterEachTest(boolean resetMappingsAfterEachTest) { + this.resetMappingsAfterEachTest = resetMappingsAfterEachTest; + } + public Server getServer() { return this.server; } diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockTestExecutionListener.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockTestExecutionListener.java index a671cd1cb3..b0785de2f7 100644 --- a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockTestExecutionListener.java +++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockTestExecutionListener.java @@ -73,7 +73,23 @@ public final class WireMockTestExecutionListener extends AbstractTestExecutionLi "Resetting mappings for the next test to restart them. That's necessary when" + " reusing the same context with new servers running on random ports"); } - wireMockConfig(testContext).resetMappings(); + wireMockConfig(testContext).reRegisterServerWithResetMappings(); + } + } + + @Override + public void afterTestMethod(TestContext testContext) throws Exception { + if (applicationContextBroken(testContext) + || wireMockConfigurationMissing(testContext) + || annotationMissing(testContext)) { + return; + } + WireMockConfiguration wireMockConfiguration = wireMockConfig(testContext); + if (wireMockConfiguration.wireMock.isResetMappingsAfterEachTest()) { + if (log.isDebugEnabled()) { + log.debug("Resetting mappings for the next test."); + } + wireMockConfiguration.resetMappings(); } } diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockWithResetAfterEachTestApplicationTests.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockWithResetAfterEachTestApplicationTests.java new file mode 100644 index 0000000000..bba91143b6 --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockWithResetAfterEachTestApplicationTests.java @@ -0,0 +1,89 @@ +/* + * Copyright 2013-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.contract.wiremock; + +import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.client.WireMock; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.client.RestTemplate; + +import static org.assertj.core.api.BDDAssertions.then; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = WiremockTestsApplication.class, + properties = { "app.baseUrl=http://localhost:${wiremock.server.port}", + "wiremock.reset-mappings-after-each-test=true" }, + webEnvironment = WebEnvironment.NONE) +@AutoConfigureWireMock(port = 0) +@FixMethodOrder +public class AutoConfigureWireMockWithResetAfterEachTestApplicationTests { + + @Autowired + private WireMockServer wireMockServer; + + @Value("localhost:${wiremock.server.port}") + private String hostname; + + @Test + public void _01_test() throws Exception { + this.wireMockServer.givenThat(WireMock.get("/should_register_mapping") + .willReturn(WireMock.aResponse().withBody("bar"))); + + String result = new RestTemplate().getForObject( + "http://" + this.hostname + "/should_register_mapping", String.class); + + then(result).isEqualTo("bar"); + } + + @Test + public void _02_test() throws Exception { + String result = new RestTemplate().getForObject( + "http://" + this.hostname + "/should_register_mapping", String.class); + + // taken from test/resources/mappings/resource-without-content-type.json + then(result).isEqualTo("Hello World"); + } + + @Test + public void _03_test() throws Exception { + WireMock.givenThat(WireMock.get("/should_register_mapping") + .willReturn(WireMock.aResponse().withBody("bar"))); + + String result = new RestTemplate().getForObject( + "http://" + this.hostname + "/should_register_mapping", String.class); + + then(result).isEqualTo("bar"); + } + + @Test + public void _04_test() throws Exception { + String result = new RestTemplate().getForObject( + "http://" + this.hostname + "/should_register_mapping", String.class); + + // taken from test/resources/mappings/resource-without-content-type.json + then(result).isEqualTo("Hello World"); + } + +}