Test auto-configured MockRestServiceServer with metrics and a root URI

Closes gh-25741
This commit is contained in:
Andy Wilkinson 2021-03-19 13:53:08 +00:00
parent bf6f36a783
commit 62023104de
3 changed files with 80 additions and 1 deletions

View File

@ -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")

View File

@ -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 {
}

View File

@ -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<String> 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();
}
}
}