From a69b207c8801e56586522656b779c14743839400 Mon Sep 17 00:00:00 2001 From: bono007 Date: Thu, 18 Feb 2021 06:40:12 -0600 Subject: [PATCH] Searches enclosing class for @AutoConfigureWiremock (#1611) * Searches enclosing class for @AutoConfigureWiremock * Replace MergedAnnotations.from with TestContextAnnotationUtils.findMergedAnnotation * Use TestContextAnnotationUtils.hasAnnotation rather than 'find and is not null' * Previous commit needs negation on "hasAnnotation" Fixes gh-1610 --- .../WireMockTestExecutionListener.java | 4 +- ...etAfterEachTestNestedApplicationTests.java | 84 +++++++++++++++++++ 2 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockWithResetAfterEachTestNestedApplicationTests.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 4f69dcd628..c463b8133a 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,9 +20,9 @@ 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.TestContextAnnotationUtils; import org.springframework.test.context.support.AbstractTestExecutionListener; /** @@ -93,7 +93,7 @@ public final class WireMockTestExecutionListener extends AbstractTestExecutionLi } private boolean annotationMissing(TestContext testContext) { - if (AnnotationUtils.findAnnotation(testContext.getTestClass(), AutoConfigureWireMock.class) == null) { + if (!TestContextAnnotationUtils.hasAnnotation(testContext.getTestClass(), AutoConfigureWireMock.class)) { 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/AutoConfigureWireMockWithResetAfterEachTestNestedApplicationTests.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockWithResetAfterEachTestNestedApplicationTests.java new file mode 100644 index 0000000000..58541a8847 --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockWithResetAfterEachTestNestedApplicationTests.java @@ -0,0 +1,84 @@ +/* + * 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 com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.client.WireMock; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +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.web.client.RestTemplate; + +import static org.assertj.core.api.BDDAssertions.then; + +@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) +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class AutoConfigureWireMockWithResetAfterEachTestNestedApplicationTests { + + @Autowired + private WireMockServer wireMockServer; + + @Value("localhost:${wiremock.server.port}") + private String hostname; + + @Test + @Order(1) + void outerTest() { + 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"); + } + + @Nested + class NestedTests { + + @Test + @Order(2) + void innerOne() { + String result = new RestTemplate().getForObject("http://" + hostname + "/should_register_mapping", + String.class); + + // taken from test/resources/mappings/resource-without-content-type.json + then(result).isEqualTo("Hello World"); + } + + @Test + @Order(3) + void innerTwo() { + WireMock.givenThat(WireMock.get("/should_register_mapping").willReturn(WireMock.aResponse().withBody("bar"))); + + String result = new RestTemplate().getForObject("http://" + hostname + "/should_register_mapping", + String.class); + + then(result).isEqualTo("bar"); + } + } +}