From bfbfe89351ae4c08d2562eecf4853b8c306c5ef9 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Tue, 8 Dec 2020 14:12:27 +0100 Subject: [PATCH 1/5] Added better logging messages for WireMock configuration --- .../wiremock/WireMockApplicationListener.java | 6 +- .../wiremock/WireMockConfiguration.java | 27 ++-- ...onfigureWireMockAdditionalImportTests.java | 139 ++++++++++++++++++ 3 files changed, 162 insertions(+), 10 deletions(-) create mode 100644 spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockAdditionalImportTests.java diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockApplicationListener.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockApplicationListener.java index 34bcb8de6e..ee8dc75cae 100644 --- a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockApplicationListener.java +++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockApplicationListener.java @@ -105,7 +105,11 @@ public class WireMockApplicationListener addPropertySource(propertySources); Map source = ((MapPropertySource) propertySources.get("wiremock")) .getSource(); - source.put(portProperty, SocketUtils.findAvailableTcpPort(minPort, maxPort)); + int port = SocketUtils.findAvailableTcpPort(minPort, maxPort); + source.put(portProperty, port); + if (log.isDebugEnabled()) { + log.debug("Registered property source for property [" + portProperty + "] with value [" + port + "]"); + } source.put(dynamicPortProperty, true); } 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 85f1601622..ead892468b 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 @@ -124,7 +124,7 @@ public class WireMockConfiguration implements SmartLifecycle { private void reRegisterBeans() { if (!this.beanFactory.containsBean(WIREMOCK_SERVER_BEAN_NAME)) { if (log.isDebugEnabled()) { - log.debug("Registering WireMock [" + this.server + "] instance"); + printRegistrationLog(); } this.beanFactory.registerSingleton(WIREMOCK_SERVER_BEAN_NAME, this.server); } @@ -136,12 +136,18 @@ public class WireMockConfiguration implements SmartLifecycle { } this.beanFactory.destroySingleton(WIREMOCK_SERVER_BEAN_NAME); if (log.isDebugEnabled()) { - log.debug("Registering WireMock [" + this.server + "] instance"); + printRegistrationLog(); } this.beanFactory.registerSingleton(WIREMOCK_SERVER_BEAN_NAME, this.server); } } + 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 + "]"); + } + private void reRegisterServer() { if (log.isTraceEnabled()) { log.trace("Creating a new server at http port [" @@ -158,14 +164,16 @@ public class WireMockConfiguration implements SmartLifecycle { else if (this.server == null) { this.server = new WireMockServer(this.options); if (log.isDebugEnabled()) { - log.debug("Created new server [" + this.server + "] at port [" - + port(this.server) + "]"); + 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 + "]"); } } start(); if (log.isDebugEnabled()) { - log.debug("Started server [" + this.server + "] at port [" + port(this.server) - + "]"); + 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 + "]"); } logRegisteredMappings(); } @@ -256,10 +264,11 @@ public class WireMockConfiguration implements SmartLifecycle { @Override public void start() { if (isRunning()) { - int port = port(this.server); + int httpPort = this.server.isRunning() ? this.server.port() : -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 port [" - + port + "]"); + log.debug("Server [" + this.server + "] is already running at http port [" + + httpPort + "] / https port [" + httpsPort + "]"); } return; } diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockAdditionalImportTests.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockAdditionalImportTests.java new file mode 100644 index 0000000000..ec74471d6d --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockAdditionalImportTests.java @@ -0,0 +1,139 @@ +/* + * Copyright 2013-2020 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 java.util.Collections; +import java.util.List; + +import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; +import com.github.tomakehurst.wiremock.client.WireMock; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import reactor.core.publisher.Mono; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.stereotype.Component; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.web.bind.annotation.GetMapping; +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}") +@AutoConfigureWireMock(port = 0) +@Import(ExtraConfig.class) +public class AutoConfigureWireMockAdditionalImportTests { + + @AfterEach + public void resetWiremock() { + WireMock.reset(); + } + + @Nested + @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = TestConfiguration.class, properties = "base-url=http://localhost:${wiremock.server.port}") + @AutoConfigureWireMock(port = 0) + class SecondControllerTest { + + @Test + void test(@Autowired WebTestClient webTestClient) { + // arrange + WireMock.stubFor(WireMock.get(WireMock.urlMatching("/find-all")) + .willReturn(ResponseDefinitionBuilder.okForJson(Collections.singletonList(new TestItem("my-name"))))); + + // act + List responseBody = webTestClient.get().uri("find-all") + .exchange().expectStatus().is2xxSuccessful() + .expectBody(new ParameterizedTypeReference>() { }).returnResult().getResponseBody(); + + // assert + Assertions.assertThat(responseBody.get(0).getName()).isEqualTo("my-name"); + } + } +} + +@Component +class ExtraConfig { + +} + +@Configuration(proxyBeanMethods = false) +@EnableAutoConfiguration +class TestConfiguration { + + @Bean + public WebClient webClient(WebClient.Builder builder) { + return builder.build(); + } + + @Bean + TestController testController(WebClient webClient, @Value("${base-url}") String baseUrl) { + return new TestController(webClient, baseUrl); + } + +} + +@RestController +class TestController { + + private final WebClient webClient; + + private final String baseUrl; + + public TestController(WebClient webClient, String baseUrl) { + this.webClient = webClient; + this.baseUrl = baseUrl; + System.out.println("Creating with URL [" + this.baseUrl + "] HASH [" + this.hashCode() + "]"); + } + + @GetMapping("find-all") + public Mono> 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>() {}); + } + +} + +class TestItem { + + private String name; + + public TestItem() { } + + public TestItem(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} \ No newline at end of file From c05e2d4dfbbda983e2a5e44d671caeafb33fae9c Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Tue, 8 Dec 2020 14:15:11 +0100 Subject: [PATCH 2/5] Fixed checkstyle --- .../wiremock/AutoConfigureWireMockAdditionalImportTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockAdditionalImportTests.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockAdditionalImportTests.java index ec74471d6d..40af33fa3a 100644 --- a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockAdditionalImportTests.java +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockAdditionalImportTests.java @@ -136,4 +136,4 @@ class TestItem { public void setName(String name) { this.name = name; } -} \ No newline at end of file +} From c46118139f58683814dac07653c6f8880582931d Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Tue, 8 Dec 2020 16:08:49 +0100 Subject: [PATCH 3/5] Fixed checkstyle --- .../AutoConfigureWireMockAdditionalImportTests.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockAdditionalImportTests.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockAdditionalImportTests.java index 40af33fa3a..2673a1e6a7 100644 --- a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockAdditionalImportTests.java +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockAdditionalImportTests.java @@ -84,7 +84,7 @@ class ExtraConfig { class TestConfiguration { @Bean - public WebClient webClient(WebClient.Builder builder) { + WebClient webClient(WebClient.Builder builder) { return builder.build(); } @@ -102,7 +102,7 @@ class TestController { private final String baseUrl; - public TestController(WebClient webClient, String baseUrl) { + TestController(WebClient webClient, String baseUrl) { this.webClient = webClient; this.baseUrl = baseUrl; System.out.println("Creating with URL [" + this.baseUrl + "] HASH [" + this.hashCode() + "]"); @@ -123,9 +123,9 @@ class TestItem { private String name; - public TestItem() { } + TestItem() { } - public TestItem(String name) { + TestItem(String name) { this.name = name; } From a0c3686292a82c43ebcafd48c672fc5c83cac012 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Wed, 9 Dec 2020 09:29:29 +0100 Subject: [PATCH 4/5] 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 --- .../wiremock/WireMockApplicationListener.java | 3 +- .../wiremock/WireMockConfiguration.java | 33 +++++++++++---- ...onfigureWireMockAdditionalImportTests.java | 40 ++++++++++++------- 3 files changed, 53 insertions(+), 23 deletions(-) diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockApplicationListener.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockApplicationListener.java index ee8dc75cae..b8d2e940ff 100644 --- a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockApplicationListener.java +++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockApplicationListener.java @@ -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); } 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 ead892468b..91d7804e9b 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 @@ -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"); } } diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockAdditionalImportTests.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockAdditionalImportTests.java index 2673a1e6a7..d3ea6d5537 100644 --- a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockAdditionalImportTests.java +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockAdditionalImportTests.java @@ -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 responseBody = webTestClient.get().uri("find-all") - .exchange().expectStatus().is2xxSuccessful() - .expectBody(new ParameterizedTypeReference>() { }).returnResult().getResponseBody(); + List responseBody = webTestClient.get().uri("find-all").exchange() + .expectStatus().is2xxSuccessful() + .expectBody(new ParameterizedTypeReference>() { + }).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> 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>() {}); + System.out.println("Will send a request to [" + this.baseUrl + "] HASH [" + + this.hashCode() + "]"); + return webClient.get().uri(baseUrl + "/find-all").retrieve() + .bodyToMono(new ParameterizedTypeReference>() { + }); } } @@ -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; } + } From 4b7234d73c6333e0b9d11e16ba37dcb13f1c07a3 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Wed, 9 Dec 2020 09:30:45 +0100 Subject: [PATCH 5/5] Revert "Revert "Support auto configuration on meta-annotations (#1564)"" This reverts commit c5b12b91b5df08f9d09c643c796a37f1ee0cffd5. --- .../WireMockTestExecutionListener.java | 6 +- ...ithResetAfterEachTestApplicationTests.java | 106 ++++++++++++++++++ 2 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/MetaAnnotationWithResetAfterEachTestApplicationTests.java 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 46bdc785fe..2179e6dbbe 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 @@ -20,6 +20,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.context.ApplicationContext; +import org.springframework.core.annotation.AnnotationUtils; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.TestContext; import org.springframework.test.context.support.AbstractTestExecutionListener; @@ -30,6 +31,7 @@ import org.springframework.test.context.support.AbstractTestExecutionListener; * @author Marcin Grzejszczak * @author Matt Garner * @author Waldemar Panas + * @author Fabian Winter * @since 1.2.6 */ public final class WireMockTestExecutionListener extends AbstractTestExecutionListener { @@ -96,8 +98,8 @@ public final class WireMockTestExecutionListener extends AbstractTestExecutionLi } private boolean annotationMissing(TestContext testContext) { - if (testContext.getTestClass() - .getAnnotationsByType(AutoConfigureWireMock.class).length == 0) { + if (AnnotationUtils.findAnnotation(testContext.getTestClass(), + AutoConfigureWireMock.class) == null) { if (log.isDebugEnabled()) { log.debug("No @AutoConfigureWireMock annotation found on [" + testContext.getTestClass() + "]. Skipping"); diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/MetaAnnotationWithResetAfterEachTestApplicationTests.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/MetaAnnotationWithResetAfterEachTestApplicationTests.java new file mode 100644 index 0000000000..bb40f0dfd3 --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/MetaAnnotationWithResetAfterEachTestApplicationTests.java @@ -0,0 +1,106 @@ +/* + * Copyright 2013-2020 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 java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +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.annotation.DirtiesContext; +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) +@ComponentTestAnnotation +@FixMethodOrder +@DirtiesContext +public class MetaAnnotationWithResetAfterEachTestApplicationTests { + + @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"); + } + +} + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Inherited +@SpringBootTest +@AutoConfigureWireMock(port = 0) +@interface ComponentTestAnnotation { + +}