Adds a flag to disable stubs after each test excution; fixes gh-1286
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user