#883: Wiremock: Fix for Spring context always being set to Dirty. (#884)

* #883: Using boolean values in Wiremock.server so that it's possible to identify if dynamic ports have been configured.

* #883: Triage feedback. Added  @author. Updated license year. Changed test name.

* #883: Fixed portIsFixed logic. Added test to check expect port status to applicable tests. @DirtiesContext to test so that context is cleared down to avoid conflict in the next test.

* #883: Removed duplicate portIsFixed/portIsNotFixed unit tests.

* #883: Checkstyle fixes.
This commit is contained in:
boothen
2019-02-17 16:22:00 +00:00
committed by Olga Maciaszek-Sharma
parent 38782b9260
commit 11965323ae
12 changed files with 82 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-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.
@@ -34,6 +34,7 @@ import org.springframework.util.SocketUtils;
* initialized. For example, by finding free ports for the server to listen on.
*
* @author Dave Syer
* @author Matt Garner
*
*/
@Order(Ordered.LOWEST_PRECEDENCE)
@@ -53,6 +54,7 @@ public class WireMockApplicationListener
.get("wiremock")).getSource();
source.put("wiremock.server.port",
SocketUtils.findAvailableTcpPort(10000, 12500));
source.put("wiremock.server.port-dynamic", true);
}
if (environment.getProperty("wiremock.server.https-port", Integer.class,
0) == 0) {
@@ -62,7 +64,17 @@ public class WireMockApplicationListener
.get("wiremock")).getSource();
source.put("wiremock.server.https-port",
SocketUtils.findAvailableTcpPort(12500, 15000));
source.put("wiremock.server.https-port-dynamic", true);
}
else if (environment.getProperty("wiremock.server.https-port", Integer.class,
0) != -1) {
MutablePropertySources propertySources = environment.getPropertySources();
addPropertySource(propertySources);
Map<String, Object> source = ((MapPropertySource) propertySources
.get("wiremock")).getSource();
source.put("wiremock.server.https-port-dynamic", false);
}
}
private void addPropertySource(MutablePropertySources propertySources) {

View File

@@ -52,6 +52,7 @@ import org.springframework.util.StringUtils;
* {@link WireMockSpring#options()}) to your test context.
*
* @author Dave Syer
* @author Matt Garner
*
*/
@Configuration
@@ -254,6 +255,10 @@ class WireMockProperties {
private String[] files;
private boolean portDynamic = false;
private boolean httpsPortDynamic = true;
public int getPort() {
return this.port;
}
@@ -286,6 +291,22 @@ class WireMockProperties {
this.files = files;
}
public boolean isPortDynamic() {
return portDynamic;
}
public void setPortDynamic(boolean portDynamic) {
this.portDynamic = portDynamic;
}
public boolean isHttpsPortDynamic() {
return httpsPortDynamic;
}
public void setHttpsPortDynamic(boolean httpsPortDynamic) {
this.httpsPortDynamic = httpsPortDynamic;
}
}
}

View File

@@ -28,6 +28,7 @@ import org.springframework.test.context.support.AbstractTestExecutionListener;
* Dirties the test context if WireMock was running on a fixed port.
*
* @author Marcin Grzejszczak
* @author Matt Garner
* @since 1.2.6
*/
public final class WireMockTestExecutionListener extends AbstractTestExecutionListener {
@@ -45,7 +46,7 @@ public final class WireMockTestExecutionListener extends AbstractTestExecutionLi
if (log.isWarnEnabled()) {
log.warn("You've used fixed ports for WireMock setup - "
+ "will mark context as dirty. Please use random ports, as much "
+ "as possible. Your tests will be faster and more reliable and this"
+ "as possible. Your tests will be faster and more reliable and this "
+ "warning will go away");
}
testContext
@@ -97,9 +98,10 @@ public final class WireMockTestExecutionListener extends AbstractTestExecutionLi
private boolean portIsFixed(TestContext testContext) {
WireMockConfiguration wireMockProperties = wireMockConfig(testContext);
int httpPort = wireMockProperties.wireMock.getServer().getPort();
int httpsPort = wireMockProperties.wireMock.getServer().getHttpsPort();
return (httpPort != 0 || httpsPort != -1) && httpsPort != 0;
boolean httpPortDynamic = wireMockProperties.wireMock.getServer().isPortDynamic();
boolean httpsPortDynamic = wireMockProperties.wireMock.getServer()
.isHttpsPortDynamic();
return !httpPortDynamic || !httpsPortDynamic;
}
}

View File

@@ -38,6 +38,9 @@ public class AutoConfigureWireMockApplicationTests {
@Autowired
private Service service;
@Autowired
private WireMockProperties wireMockProperties;
@Test
public void contextLoads() throws Exception {
stubFor(get(urlEqualTo("/test")).willReturn(aResponse()
@@ -45,4 +48,11 @@ public class AutoConfigureWireMockApplicationTests {
assertThat(this.service.go()).isEqualTo("Hello World!");
}
@Test
public void portsAreFixed() {
boolean httpPortDynamic = wireMockProperties.getServer().isPortDynamic();
boolean httpsPortDynamic = wireMockProperties.getServer().isHttpsPortDynamic();
assertThat(!httpPortDynamic || !httpsPortDynamic).isTrue();
}
}

View File

@@ -35,9 +35,19 @@ public class AutoConfigureWireMockAutoStubsApplicationTests {
@Autowired
private Service service;
@Autowired
private WireMockProperties wireMockProperties;
@Test
public void contextLoads() throws Exception {
assertThat(this.service.go()).isEqualTo("Hello World");
}
@Test
public void portsAreNotFixed() {
boolean httpPortDynamic = wireMockProperties.getServer().isPortDynamic();
boolean httpsPortDynamic = wireMockProperties.getServer().isHttpsPortDynamic();
assertThat(!httpPortDynamic || !httpsPortDynamic).isFalse();
}
}

View File

@@ -39,5 +39,4 @@ public class AutoConfigureWireMockFilesApplicationTests {
assertThat(this.service.go())
.isEqualToIgnoringWhitespace("{\"message\":\"Hello Root\"}");
}
}

View File

@@ -38,6 +38,9 @@ public class AutoConfigureWireMockHttpsPortApplicationTests {
@Autowired
private Service service;
@Autowired
private WireMockProperties wireMockProperties;
@Test
public void contextLoads() throws Exception {
stubFor(get(urlEqualTo("/test")).willReturn(aResponse()
@@ -45,4 +48,11 @@ public class AutoConfigureWireMockHttpsPortApplicationTests {
assertThat(this.service.go()).isEqualTo("Hello World!");
}
@Test
public void portsAreFixed() {
boolean httpPortDynamic = wireMockProperties.getServer().isPortDynamic();
boolean httpsPortDynamic = wireMockProperties.getServer().isHttpsPortDynamic();
assertThat(!httpPortDynamic || !httpsPortDynamic).isTrue();
}
}

View File

@@ -22,6 +22,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.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
@@ -30,6 +31,7 @@ import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@RunWith(SpringRunner.class)
@SpringBootTest(classes = WiremockTestsApplication.class, properties = "app.baseUrl=http://localhost:${wiremock.server.port}", webEnvironment = WebEnvironment.NONE)
@AutoConfigureWireMock(port = 0)
@@ -44,5 +46,4 @@ public class AutoConfigureWireMockRandomPortApplicationTests {
.withHeader("Content-Type", "text/plain").withBody("Hello World!")));
assertThat(this.service.go()).isEqualTo("Hello World!");
}
}

View File

@@ -38,6 +38,9 @@ public class AutoConfigureWireMockRandomPortHttpsApplicationTests {
@Autowired
private Service service;
@Autowired
private WireMockProperties wireMockProperties;
@Test
public void contextLoads() throws Exception {
stubFor(get(urlEqualTo("/test")).willReturn(aResponse()
@@ -45,4 +48,11 @@ public class AutoConfigureWireMockRandomPortHttpsApplicationTests {
assertThat(this.service.go()).isEqualTo("Hello World!");
}
@Test
public void portsAreNotFixed() {
boolean httpPortDynamic = wireMockProperties.getServer().isPortDynamic();
boolean httpsPortDynamic = wireMockProperties.getServer().isHttpsPortDynamic();
assertThat(!httpPortDynamic || !httpsPortDynamic).isFalse();
}
}

View File

@@ -45,5 +45,4 @@ public class AutoConfigureWireMockSamePortApplicationTests {
.withHeader("Content-Type", "text/plain").withBody("Hello World2!")));
assertThat(this.service.go2()).isEqualTo("Hello World2!");
}
}

View File

@@ -40,5 +40,4 @@ public class AutoConfigureWireMockStubsAndMultipleFilesApplicationTests {
assertThat(this.service.go())
.isEqualToIgnoringWhitespace("{\"message\":\"Hello Root\"}");
}
}

View File

@@ -38,5 +38,4 @@ public class AutoConfigureWireMockStubsApplicationWithSlashTests {
public void contextLoads() throws Exception {
assertThat(this.service.go()).isEqualTo("Hello World");
}
}