From 62023104deb6315cfc385b0447a689b2512dfa36 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 19 Mar 2021 13:53:08 +0000 Subject: [PATCH] Test auto-configured MockRestServiceServer with metrics and a root URI Closes gh-25741 --- .../build.gradle | 2 + .../restdocs/RestDocsTestApplication.java | 3 +- ...viceServerWithRootUriIntegrationTests.java | 76 +++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/AutoConfigureMockRestServiceServerWithRootUriIntegrationTests.java diff --git a/spring-boot-project/spring-boot-test-autoconfigure/build.gradle b/spring-boot-project/spring-boot-test-autoconfigure/build.gradle index a05afc1ba9b..5ac749a3f5e 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/build.gradle +++ b/spring-boot-project/spring-boot-test-autoconfigure/build.gradle @@ -48,12 +48,14 @@ dependencies { optional("org.mongodb:mongodb-driver-reactivestreams") optional("org.mongodb:mongodb-driver-sync") + testImplementation(project(":spring-boot-project:spring-boot-actuator-autoconfigure")) testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support")) testImplementation("ch.qos.logback:logback-classic") testImplementation("com.fasterxml.jackson.module:jackson-module-parameter-names") testImplementation("com.h2database:h2") testImplementation("com.unboundid:unboundid-ldapsdk") testImplementation("io.lettuce:lettuce-core") + testImplementation("io.micrometer:micrometer-core") testImplementation("io.projectreactor:reactor-core") testImplementation("io.projectreactor:reactor-test") testImplementation("io.r2dbc:r2dbc-h2") diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsTestApplication.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsTestApplication.java index b90c7e9706e..c62c85d0646 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsTestApplication.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsTestApplication.java @@ -16,6 +16,7 @@ package org.springframework.boot.test.autoconfigure.restdocs; +import org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; @@ -24,7 +25,7 @@ import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfi * * @author Andy Wilkinson */ -@SpringBootApplication(exclude = SecurityAutoConfiguration.class) +@SpringBootApplication(exclude = { SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class }) public class RestDocsTestApplication { } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/AutoConfigureMockRestServiceServerWithRootUriIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/AutoConfigureMockRestServiceServerWithRootUriIntegrationTests.java new file mode 100644 index 00000000000..a003ddec53e --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/AutoConfigureMockRestServiceServerWithRootUriIntegrationTests.java @@ -0,0 +1,76 @@ +/* + * Copyright 2012-2021 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.boot.test.autoconfigure.web.client; + +import io.micrometer.core.instrument.MeterRegistry; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.test.web.client.MockRestServiceServer; +import org.springframework.web.client.RestTemplate; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; +import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; + +/** + * Tests for + * {@link AutoConfigureMockRestServiceServer @AutoConfigureMockRestServiceServer} with a + * {@link RestTemplate} configured with a root URI. + * + * @author Andy Wilkinson + */ +@SpringBootTest(properties = "debug=true") +@AutoConfigureMockRestServiceServer +class AutoConfigureMockRestServiceServerWithRootUriIntegrationTests { + + @Autowired + private RestTemplate restTemplate; + + @Autowired + private MockRestServiceServer server; + + @Autowired + MeterRegistry meterRegistry; + + @Test + void whenRestTemplateAppliesARootUriThenMockServerExpecationsAreStillMatched() { + this.server.expect(requestTo("/test")).andRespond(withSuccess("hello", MediaType.TEXT_HTML)); + ResponseEntity entity = this.restTemplate.getForEntity("/test", String.class); + assertThat(entity.getBody()).isEqualTo("hello"); + assertThat(this.meterRegistry.find("http.client.requests").tag("uri", "/rest/test").timer()).isNotNull(); + } + + @EnableAutoConfiguration + @Configuration(proxyBeanMethods = false) + static class RootUriConfiguration { + + @Bean + RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) { + return restTemplateBuilder.rootUri("/rest").build(); + } + + } + +}