Setting current static WireMock instance when server is already running
without this change when the server was already running we wouldn't set the static WireMock's instance to that running server. That would result in still pointing to a previously started WireMock instance with this change we're setting the static server instance to the running server fixes gh-1425
This commit is contained in:
@@ -108,7 +108,8 @@ public class WireMockApplicationListener
|
||||
int port = SocketUtils.findAvailableTcpPort(minPort, maxPort);
|
||||
source.put(portProperty, port);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Registered property source for property [" + portProperty + "] with value [" + port + "]");
|
||||
log.debug("Registered property source for property [" + portProperty
|
||||
+ "] with value [" + port + "]");
|
||||
}
|
||||
source.put(dynamicPortProperty, true);
|
||||
}
|
||||
|
||||
@@ -144,8 +144,11 @@ public class WireMockConfiguration implements SmartLifecycle {
|
||||
|
||||
private void printRegistrationLog() {
|
||||
int httpPort = this.server.isRunning() ? this.server.port() : -1;
|
||||
int httpsPort = this.server.isRunning() && this.server.getOptions().httpsSettings().enabled() ? this.server.httpsPort() : -1;
|
||||
log.debug("Registering WireMock [" + this.server + "] at http port [" + httpPort + "] and https port [" + httpsPort + "]");
|
||||
int httpsPort = this.server.isRunning()
|
||||
&& this.server.getOptions().httpsSettings().enabled()
|
||||
? this.server.httpsPort() : -1;
|
||||
log.debug("Registering WireMock [" + this.server + "] at http port [" + httpPort
|
||||
+ "] and https port [" + httpsPort + "]");
|
||||
}
|
||||
|
||||
private void reRegisterServer() {
|
||||
@@ -165,15 +168,21 @@ public class WireMockConfiguration implements SmartLifecycle {
|
||||
this.server = new WireMockServer(this.options);
|
||||
if (log.isDebugEnabled()) {
|
||||
int httpPort = this.server.isRunning() ? this.server.port() : -1;
|
||||
int httpsPort = this.server.isRunning() && this.server.getOptions().httpsSettings().enabled() ? this.server.httpsPort() : -1;
|
||||
log.debug("Created new server [" + this.server + "] at http port [" + httpPort + "] and https port [" + httpsPort + "]");
|
||||
int httpsPort = this.server.isRunning()
|
||||
&& this.server.getOptions().httpsSettings().enabled()
|
||||
? this.server.httpsPort() : -1;
|
||||
log.debug("Created new server [" + this.server + "] at http port ["
|
||||
+ httpPort + "] and https port [" + httpsPort + "]");
|
||||
}
|
||||
}
|
||||
start();
|
||||
if (log.isDebugEnabled()) {
|
||||
int httpPort = this.server.isRunning() ? this.server.port() : -1;
|
||||
int httpsPort = this.server.isRunning() && this.server.getOptions().httpsSettings().enabled() ? this.server.httpsPort() : -1;
|
||||
log.debug("Started server [" + this.server + "] at http port [" + httpPort + "] and https port [" + httpsPort + "]");
|
||||
int httpsPort = this.server.isRunning()
|
||||
&& this.server.getOptions().httpsSettings().enabled()
|
||||
? this.server.httpsPort() : -1;
|
||||
log.debug("Started server [" + this.server + "] at http port [" + httpPort
|
||||
+ "] and https port [" + httpsPort + "]");
|
||||
}
|
||||
logRegisteredMappings();
|
||||
}
|
||||
@@ -265,11 +274,14 @@ public class WireMockConfiguration implements SmartLifecycle {
|
||||
public void start() {
|
||||
if (isRunning()) {
|
||||
int httpPort = this.server.isRunning() ? this.server.port() : -1;
|
||||
int httpsPort = this.server.isRunning() && this.server.getOptions().httpsSettings().enabled() ? this.server.httpsPort() : -1;
|
||||
int httpsPort = this.server.isRunning()
|
||||
&& this.server.getOptions().httpsSettings().enabled()
|
||||
? this.server.httpsPort() : -1;
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Server [" + this.server + "] is already running at http port ["
|
||||
+ httpPort + "] / https port [" + httpsPort + "]");
|
||||
}
|
||||
updateCurrentServer();
|
||||
return;
|
||||
}
|
||||
this.server.start();
|
||||
@@ -280,7 +292,12 @@ public class WireMockConfiguration implements SmartLifecycle {
|
||||
WireMock.configureFor(new WireMock(this.server));
|
||||
this.running = true;
|
||||
if (log.isDebugEnabled() && this.server.isRunning()) {
|
||||
log.debug("Started WireMock at port [" + this.server.port() + "]. It has ["
|
||||
int httpPort = this.server.isRunning() ? this.server.port() : -1;
|
||||
int httpsPort = this.server.isRunning()
|
||||
&& this.server.getOptions().httpsSettings().enabled()
|
||||
? this.server.httpsPort() : -1;
|
||||
log.debug("Server [" + this.server + "] is already running at http port ["
|
||||
+ httpPort + "] / https port [" + httpsPort + "]. It has ["
|
||||
+ this.server.getStubMappings().size() + "] mappings registered");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +42,9 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
// issue 1541
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = TestConfiguration.class, properties = "base-url=http://localhost:${wiremock.server.port}")
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||
classes = TestConfiguration.class,
|
||||
properties = "base-url=http://localhost:${wiremock.server.port}")
|
||||
@AutoConfigureWireMock(port = 0)
|
||||
@Import(ExtraConfig.class)
|
||||
public class AutoConfigureWireMockAdditionalImportTests {
|
||||
@@ -53,7 +55,9 @@ public class AutoConfigureWireMockAdditionalImportTests {
|
||||
}
|
||||
|
||||
@Nested
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = TestConfiguration.class, properties = "base-url=http://localhost:${wiremock.server.port}")
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||
classes = TestConfiguration.class,
|
||||
properties = "base-url=http://localhost:${wiremock.server.port}")
|
||||
@AutoConfigureWireMock(port = 0)
|
||||
class SecondControllerTest {
|
||||
|
||||
@@ -61,17 +65,21 @@ public class AutoConfigureWireMockAdditionalImportTests {
|
||||
void test(@Autowired WebTestClient webTestClient) {
|
||||
// arrange
|
||||
WireMock.stubFor(WireMock.get(WireMock.urlMatching("/find-all"))
|
||||
.willReturn(ResponseDefinitionBuilder.okForJson(Collections.singletonList(new TestItem("my-name")))));
|
||||
.willReturn(ResponseDefinitionBuilder.okForJson(
|
||||
Collections.singletonList(new TestItem("my-name")))));
|
||||
|
||||
// act
|
||||
List<TestItem> responseBody = webTestClient.get().uri("find-all")
|
||||
.exchange().expectStatus().is2xxSuccessful()
|
||||
.expectBody(new ParameterizedTypeReference<List<TestItem>>() { }).returnResult().getResponseBody();
|
||||
List<TestItem> responseBody = webTestClient.get().uri("find-all").exchange()
|
||||
.expectStatus().is2xxSuccessful()
|
||||
.expectBody(new ParameterizedTypeReference<List<TestItem>>() {
|
||||
}).returnResult().getResponseBody();
|
||||
|
||||
// assert
|
||||
Assertions.assertThat(responseBody.get(0).getName()).isEqualTo("my-name");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Component
|
||||
@@ -89,7 +97,8 @@ class TestConfiguration {
|
||||
}
|
||||
|
||||
@Bean
|
||||
TestController testController(WebClient webClient, @Value("${base-url}") String baseUrl) {
|
||||
TestController testController(WebClient webClient,
|
||||
@Value("${base-url}") String baseUrl) {
|
||||
return new TestController(webClient, baseUrl);
|
||||
}
|
||||
|
||||
@@ -105,16 +114,17 @@ class TestController {
|
||||
TestController(WebClient webClient, String baseUrl) {
|
||||
this.webClient = webClient;
|
||||
this.baseUrl = baseUrl;
|
||||
System.out.println("Creating with URL [" + this.baseUrl + "] HASH [" + this.hashCode() + "]");
|
||||
System.out.println("Creating with URL [" + this.baseUrl + "] HASH ["
|
||||
+ this.hashCode() + "]");
|
||||
}
|
||||
|
||||
@GetMapping("find-all")
|
||||
public Mono<List<TestItem>> findAll() {
|
||||
System.out.println("Will send a request to [" + this.baseUrl + "] HASH [" + this.hashCode() + "]");
|
||||
return webClient
|
||||
.get()
|
||||
.uri(baseUrl + "/find-all")
|
||||
.retrieve().bodyToMono(new ParameterizedTypeReference<List<TestItem>>() {});
|
||||
System.out.println("Will send a request to [" + this.baseUrl + "] HASH ["
|
||||
+ this.hashCode() + "]");
|
||||
return webClient.get().uri(baseUrl + "/find-all").retrieve()
|
||||
.bodyToMono(new ParameterizedTypeReference<List<TestItem>>() {
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -123,7 +133,8 @@ class TestItem {
|
||||
|
||||
private String name;
|
||||
|
||||
TestItem() { }
|
||||
TestItem() {
|
||||
}
|
||||
|
||||
TestItem(String name) {
|
||||
this.name = name;
|
||||
@@ -136,4 +147,5 @@ class TestItem {
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user