From e3646988fe08882ae0efe57a7f02d16ba9e98113 Mon Sep 17 00:00:00 2001 From: Fabian Winter <5821180+fdw@users.noreply.github.com> Date: Mon, 23 Nov 2020 10:27:56 +0100 Subject: [PATCH 1/2] Support auto configuration on meta-annotations (#1564) With this change, the stubbing of WireMock will be reset even if only a meta-annotation is annotated with `@AutoConfigureWireMock`. Fixes gh-1204 --- .../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 { + +} From c5b12b91b5df08f9d09c643c796a37f1ee0cffd5 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Tue, 24 Nov 2020 08:33:04 +0100 Subject: [PATCH 2/2] Revert "Support auto configuration on meta-annotations (#1564)" This reverts commit e3646988fe08882ae0efe57a7f02d16ba9e98113. --- .../WireMockTestExecutionListener.java | 6 +- ...ithResetAfterEachTestApplicationTests.java | 106 ------------------ 2 files changed, 2 insertions(+), 110 deletions(-) delete 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 2179e6dbbe..46bdc785fe 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,7 +20,6 @@ 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; @@ -31,7 +30,6 @@ 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 { @@ -98,8 +96,8 @@ public final class WireMockTestExecutionListener extends AbstractTestExecutionLi } private boolean annotationMissing(TestContext testContext) { - if (AnnotationUtils.findAnnotation(testContext.getTestClass(), - AutoConfigureWireMock.class) == null) { + if (testContext.getTestClass() + .getAnnotationsByType(AutoConfigureWireMock.class).length == 0) { 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 deleted file mode 100644 index bb40f0dfd3..0000000000 --- a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/MetaAnnotationWithResetAfterEachTestApplicationTests.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * 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 { - -}