Configure a RestClient.Builder with RestClientTest
This commit adds support for configuring a `RestClient.Builder` and `MockRestServiceServer` support for the `RestClient` when using `@RestClientTest` sliced tests. Closes gh-37033pull/37746/head
parent
d725914cd5
commit
ff99de49c4
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2012-2023 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.docs.features.testing.springbootapplications.autoconfiguredrestclient;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.client.RestClientTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.client.MockRestServiceServer;
|
||||
|
||||
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;
|
||||
|
||||
@RestClientTest(RemoteVehicleDetailsService.class)
|
||||
class MyRestClientServiceTests {
|
||||
|
||||
@Autowired
|
||||
private RemoteVehicleDetailsService service;
|
||||
|
||||
@Autowired
|
||||
private MockRestServiceServer server;
|
||||
|
||||
@Test
|
||||
void getVehicleDetailsWhenResultIsSuccessShouldReturnDetails() {
|
||||
this.server.expect(requestTo("https://example.com/greet/details"))
|
||||
.andRespond(withSuccess("hello", MediaType.TEXT_PLAIN));
|
||||
String greeting = this.service.callRestService();
|
||||
assertThat(greeting).isEqualTo("hello");
|
||||
}
|
||||
|
||||
}
|
4
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredrestclient/MyRestClientTests.java → spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredrestclient/MyRestTemplateServiceTests.java
4
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredrestclient/MyRestClientTests.java → spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredrestclient/MyRestTemplateServiceTests.java
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2012-2023 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.docs.features.testing.springbootapplications.autoconfiguredrestclient
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.boot.test.autoconfigure.web.client.RestClientTest
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.test.web.client.MockRestServiceServer
|
||||
import org.springframework.test.web.client.match.MockRestRequestMatchers
|
||||
import org.springframework.test.web.client.response.MockRestResponseCreators
|
||||
|
||||
@RestClientTest(RemoteVehicleDetailsService::class)
|
||||
class MyRestClientServiceTests(
|
||||
@Autowired val service: RemoteVehicleDetailsService,
|
||||
@Autowired val server: MockRestServiceServer) {
|
||||
|
||||
@Test
|
||||
fun getVehicleDetailsWhenResultIsSuccessShouldReturnDetails(): Unit {
|
||||
server.expect(MockRestRequestMatchers.requestTo("https://example.com/greet/details"))
|
||||
.andRespond(MockRestResponseCreators.withSuccess("hello", MediaType.TEXT_PLAIN))
|
||||
val greeting = service.callRestService()
|
||||
assertThat(greeting).isEqualTo("hello")
|
||||
}
|
||||
|
||||
}
|
4
spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredrestclient/MyRestClientTests.kt → spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredrestclient/MyRestTemplateServiceTests.kt
4
spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredrestclient/MyRestClientTests.kt → spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredrestclient/MyRestTemplateServiceTests.kt
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2012-2023 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 org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestClient;
|
||||
import org.springframework.web.client.RestClient.Builder;
|
||||
|
||||
/**
|
||||
* A second example web client used with {@link RestClientTest @RestClientTest} tests.
|
||||
*
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
@Service
|
||||
public class AnotherExampleRestClientService {
|
||||
|
||||
private final Builder builder;
|
||||
|
||||
private final RestClient restClient;
|
||||
|
||||
public AnotherExampleRestClientService(RestClient.Builder builder) {
|
||||
this.builder = builder;
|
||||
this.restClient = builder.baseUrl("https://example.com").build();
|
||||
}
|
||||
|
||||
protected Builder getRestClientBuilder() {
|
||||
return this.builder;
|
||||
}
|
||||
|
||||
public String test() {
|
||||
return this.restClient.get().uri("/test").retrieve().toEntity(String.class).getBody();
|
||||
}
|
||||
|
||||
}
|
6
spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/AnotherExampleRestClient.java → spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/AnotherExampleRestTemplateService.java
6
spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/AnotherExampleRestClient.java → spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/AnotherExampleRestTemplateService.java
@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2012-2023 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 org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
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.RestClient;
|
||||
import org.springframework.web.client.RestClient.Builder;
|
||||
|
||||
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 RestClient} configured with a base URL.
|
||||
*
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockRestServiceServer
|
||||
class AutoConfigureMockRestServiceServerWithRestClientIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private RestClient restClient;
|
||||
|
||||
@Autowired
|
||||
private MockRestServiceServer server;
|
||||
|
||||
@Test
|
||||
void mockServerExpectationsAreMatched() {
|
||||
this.server.expect(requestTo("/rest/test")).andRespond(withSuccess("hello", MediaType.TEXT_HTML));
|
||||
ResponseEntity<String> entity = this.restClient.get().uri("/test").retrieve().toEntity(String.class);
|
||||
assertThat(entity.getBody()).isEqualTo("hello");
|
||||
}
|
||||
|
||||
@EnableAutoConfiguration(exclude = CassandraAutoConfiguration.class)
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class RootUriConfiguration {
|
||||
|
||||
@Bean
|
||||
RestClient restClient(Builder restClientBuilder) {
|
||||
return restClientBuilder.baseUrl("/rest").build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
4
spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/AutoConfigureMockRestServiceServerWithRootUriIntegrationTests.java → spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/AutoConfigureMockRestServiceServerWithRestTemplateRootUriIntegrationTests.java
4
spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/AutoConfigureMockRestServiceServerWithRootUriIntegrationTests.java → spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/AutoConfigureMockRestServiceServerWithRestTemplateRootUriIntegrationTests.java
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2012-2023 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 org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestClient;
|
||||
import org.springframework.web.client.RestClient.Builder;
|
||||
|
||||
/**
|
||||
* Example web client using {@code RestClient} with {@link RestClientTest @RestClientTest}
|
||||
* tests.
|
||||
*
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
@Service
|
||||
public class ExampleRestClientService {
|
||||
|
||||
private final Builder builder;
|
||||
|
||||
private final RestClient restClient;
|
||||
|
||||
public ExampleRestClientService(RestClient.Builder builder) {
|
||||
this.builder = builder;
|
||||
this.restClient = builder.baseUrl("https://example.com").build();
|
||||
}
|
||||
|
||||
protected Builder getRestClientBuilder() {
|
||||
return this.builder;
|
||||
}
|
||||
|
||||
public String test() {
|
||||
return this.restClient.get().uri("/test").retrieve().toEntity(String.class).getBody();
|
||||
}
|
||||
|
||||
public void testPostWithBody(String body) {
|
||||
this.restClient.post().uri("/test").body(body).retrieve().toBodilessEntity();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2012-2023 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 org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.client.MockRestServiceServer;
|
||||
import org.springframework.web.client.RestClient;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.content;
|
||||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
|
||||
|
||||
/**
|
||||
* Tests for {@link RestClientTest @RestClientTest} with a {@link RestClient}.
|
||||
*
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
@RestClientTest(ExampleRestClientService.class)
|
||||
class RestClientTestRestClientIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private MockRestServiceServer server;
|
||||
|
||||
@Autowired
|
||||
private ExampleRestClientService client;
|
||||
|
||||
@Test
|
||||
void mockServerCall1() {
|
||||
this.server.expect(requestTo(uri("/test"))).andRespond(withSuccess("1", MediaType.TEXT_HTML));
|
||||
assertThat(this.client.test()).isEqualTo("1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void mockServerCall2() {
|
||||
this.server.expect(requestTo(uri("/test"))).andRespond(withSuccess("2", MediaType.TEXT_HTML));
|
||||
assertThat(this.client.test()).isEqualTo("2");
|
||||
}
|
||||
|
||||
@Test
|
||||
void mockServerCallWithContent() {
|
||||
this.server.expect(requestTo(uri("/test")))
|
||||
.andExpect(content().string("test"))
|
||||
.andRespond(withSuccess("1", MediaType.TEXT_HTML));
|
||||
this.client.testPostWithBody("test");
|
||||
}
|
||||
|
||||
private static String uri(String path) {
|
||||
return "https://example.com" + path;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 2012-2023 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 org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.web.client.MockServerRestClientCustomizer;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.client.MockRestServiceServer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
|
||||
|
||||
/**
|
||||
* Tests for {@link RestClientTest @RestClientTest} with two {@code RestClient} clients.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
@RestClientTest({ ExampleRestClientService.class, AnotherExampleRestClientService.class })
|
||||
class RestClientTestRestClientTwoComponentsIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private ExampleRestClientService client1;
|
||||
|
||||
@Autowired
|
||||
private AnotherExampleRestClientService client2;
|
||||
|
||||
@Autowired
|
||||
private MockServerRestClientCustomizer customizer;
|
||||
|
||||
@Autowired
|
||||
private MockRestServiceServer server;
|
||||
|
||||
@Test
|
||||
void serverShouldNotWork() {
|
||||
assertThatIllegalStateException().isThrownBy(
|
||||
() -> this.server.expect(requestTo(uri("/test"))).andRespond(withSuccess("hello", MediaType.TEXT_HTML)))
|
||||
.withMessageContaining("Unable to use auto-configured");
|
||||
}
|
||||
|
||||
@Test
|
||||
void client1RestCallViaCustomizer() {
|
||||
this.customizer.getServer(this.client1.getRestClientBuilder())
|
||||
.expect(requestTo(uri("/test")))
|
||||
.andRespond(withSuccess("hello", MediaType.TEXT_HTML));
|
||||
assertThat(this.client1.test()).isEqualTo("hello");
|
||||
}
|
||||
|
||||
@Test
|
||||
void client2RestCallViaCustomizer() {
|
||||
this.customizer.getServer(this.client2.getRestClientBuilder())
|
||||
.expect(requestTo(uri("/test")))
|
||||
.andRespond(withSuccess("there", MediaType.TEXT_HTML));
|
||||
assertThat(this.client2.test()).isEqualTo("there");
|
||||
}
|
||||
|
||||
private static String uri(String path) {
|
||||
return "https://example.com" + path;
|
||||
}
|
||||
|
||||
}
|
6
spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/RestClientRestIntegrationTests.java → spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/RestClientTestRestTemplateIntegrationTests.java
6
spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/RestClientRestIntegrationTests.java → spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/RestClientTestRestTemplateIntegrationTests.java
10
spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/RestClientTestTwoComponentsIntegrationTests.java → spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/RestClientTestRestTemplateTwoComponentsIntegrationTests.java
10
spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/RestClientTestTwoComponentsIntegrationTests.java → spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/RestClientTestRestTemplateTwoComponentsIntegrationTests.java
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2012-2023 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 org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.client.MockRestServiceServer;
|
||||
|
||||
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 RestClientTest @RestClientTest} with a single client using
|
||||
* {@code RestClient}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
@RestClientTest(ExampleRestClientService.class)
|
||||
class RestClientTestWithRestClientComponentIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private MockRestServiceServer server;
|
||||
|
||||
@Autowired
|
||||
private ExampleRestClientService client;
|
||||
|
||||
@Test
|
||||
void mockServerCall() {
|
||||
this.server.expect(requestTo("https://example.com/test")).andRespond(withSuccess("hello", MediaType.TEXT_HTML));
|
||||
assertThat(this.client.test()).isEqualTo("hello");
|
||||
}
|
||||
|
||||
}
|
11
spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/RestClientTestWithComponentIntegrationTests.java → spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/RestClientTestWithRestTemplateComponentIntegrationTests.java
11
spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/RestClientTestWithComponentIntegrationTests.java → spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/RestClientTestWithRestTemplateComponentIntegrationTests.java
@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Copyright 2012-2023 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.web.client;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.boot.web.client.RestClientCustomizer;
|
||||
import org.springframework.http.client.BufferingClientHttpRequestFactory;
|
||||
import org.springframework.test.web.client.MockRestServiceServer;
|
||||
import org.springframework.test.web.client.MockRestServiceServer.MockRestServiceServerBuilder;
|
||||
import org.springframework.test.web.client.RequestExpectationManager;
|
||||
import org.springframework.test.web.client.SimpleRequestExpectationManager;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.client.RestClient;
|
||||
|
||||
/**
|
||||
* {@link RestClientCustomizer} that can be applied to {@link RestClient.Builder}
|
||||
* instances to add {@link MockRestServiceServer} support.
|
||||
* <p>
|
||||
* Typically applied to an existing builder before it is used, for example:
|
||||
* <pre class="code">
|
||||
* MockServerRestClientCustomizer customizer = new MockServerRestClientCustomizer();
|
||||
* RestClient.Builder builder = RestClient.builder();
|
||||
* customizer.customize(builder);
|
||||
* MyBean bean = new MyBean(client.build());
|
||||
* customizer.getServer().expect(requestTo("/hello")).andRespond(withSuccess());
|
||||
* bean.makeRestCall();
|
||||
* </pre>
|
||||
* <p>
|
||||
* If the customizer is only used once, the {@link #getServer()} method can be used to
|
||||
* obtain the mock server. If the customizer has been used more than once the
|
||||
* {@link #getServer(RestClient.Builder)} or {@link #getServers()} method must be used to
|
||||
* access the related server.
|
||||
*
|
||||
* @author Scott Frederick
|
||||
* @since 3.2.0
|
||||
* @see #getServer()
|
||||
* @see #getServer(RestClient.Builder)
|
||||
*/
|
||||
public class MockServerRestClientCustomizer implements RestClientCustomizer {
|
||||
|
||||
private final Map<RestClient.Builder, RequestExpectationManager> expectationManagers = new ConcurrentHashMap<>();
|
||||
|
||||
private final Map<RestClient.Builder, MockRestServiceServer> servers = new ConcurrentHashMap<>();
|
||||
|
||||
private final Supplier<? extends RequestExpectationManager> expectationManagerSupplier;
|
||||
|
||||
private boolean bufferContent = false;
|
||||
|
||||
public MockServerRestClientCustomizer() {
|
||||
this(SimpleRequestExpectationManager::new);
|
||||
}
|
||||
|
||||
/**
|
||||
* Crate a new {@link MockServerRestClientCustomizer} instance.
|
||||
* @param expectationManager the expectation manager class to use
|
||||
*/
|
||||
public MockServerRestClientCustomizer(Class<? extends RequestExpectationManager> expectationManager) {
|
||||
this(() -> BeanUtils.instantiateClass(expectationManager));
|
||||
Assert.notNull(expectationManager, "ExpectationManager must not be null");
|
||||
}
|
||||
|
||||
/**
|
||||
* Crate a new {@link MockServerRestClientCustomizer} instance.
|
||||
* @param expectationManagerSupplier a supplier that provides the
|
||||
* {@link RequestExpectationManager} to use
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public MockServerRestClientCustomizer(Supplier<? extends RequestExpectationManager> expectationManagerSupplier) {
|
||||
Assert.notNull(expectationManagerSupplier, "ExpectationManagerSupplier must not be null");
|
||||
this.expectationManagerSupplier = expectationManagerSupplier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set if the {@link BufferingClientHttpRequestFactory} wrapper should be used to
|
||||
* buffer the input and output streams, and for example, allow multiple reads of the
|
||||
* response body.
|
||||
* @param bufferContent if request and response content should be buffered
|
||||
* @since 3.1.0
|
||||
*/
|
||||
public void setBufferContent(boolean bufferContent) {
|
||||
this.bufferContent = bufferContent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void customize(RestClient.Builder restClientBuilder) {
|
||||
RequestExpectationManager expectationManager = createExpectationManager();
|
||||
MockRestServiceServerBuilder serverBuilder = MockRestServiceServer.bindTo(restClientBuilder);
|
||||
if (this.bufferContent) {
|
||||
serverBuilder.bufferContent();
|
||||
}
|
||||
MockRestServiceServer server = serverBuilder.build(expectationManager);
|
||||
this.expectationManagers.put(restClientBuilder, expectationManager);
|
||||
this.servers.put(restClientBuilder, server);
|
||||
}
|
||||
|
||||
protected RequestExpectationManager createExpectationManager() {
|
||||
return this.expectationManagerSupplier.get();
|
||||
}
|
||||
|
||||
public MockRestServiceServer getServer() {
|
||||
Assert.state(!this.servers.isEmpty(), "Unable to return a single MockRestServiceServer since "
|
||||
+ "MockServerRestClientCustomizer has not been bound to a RestClient");
|
||||
Assert.state(this.servers.size() == 1, "Unable to return a single MockRestServiceServer since "
|
||||
+ "MockServerRestClientCustomizer has been bound to more than one RestClient");
|
||||
return this.servers.values().iterator().next();
|
||||
}
|
||||
|
||||
public Map<RestClient.Builder, RequestExpectationManager> getExpectationManagers() {
|
||||
return this.expectationManagers;
|
||||
}
|
||||
|
||||
public MockRestServiceServer getServer(RestClient.Builder restClientBuilder) {
|
||||
return this.servers.get(restClientBuilder);
|
||||
}
|
||||
|
||||
public Map<RestClient.Builder, MockRestServiceServer> getServers() {
|
||||
return Collections.unmodifiableMap(this.servers);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,163 @@
|
||||
/*
|
||||
* Copyright 2012-2023 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.web.client;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.test.web.client.RequestExpectationManager;
|
||||
import org.springframework.test.web.client.SimpleRequestExpectationManager;
|
||||
import org.springframework.test.web.client.UnorderedRequestExpectationManager;
|
||||
import org.springframework.web.client.RestClient;
|
||||
import org.springframework.web.client.RestClient.Builder;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
|
||||
|
||||
/**
|
||||
* Tests for {@link MockServerRestClientCustomizer}.
|
||||
*
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
class MockServerRestClientCustomizerTests {
|
||||
|
||||
private MockServerRestClientCustomizer customizer;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
this.customizer = new MockServerRestClientCustomizer();
|
||||
}
|
||||
|
||||
@Test
|
||||
void createShouldUseSimpleRequestExpectationManager() {
|
||||
MockServerRestClientCustomizer customizer = new MockServerRestClientCustomizer();
|
||||
customizer.customize(RestClient.builder());
|
||||
assertThat(customizer.getServer()).extracting("expectationManager")
|
||||
.isInstanceOf(SimpleRequestExpectationManager.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWhenExpectationManagerClassIsNullShouldThrowException() {
|
||||
Class<? extends RequestExpectationManager> expectationManager = null;
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new MockServerRestClientCustomizer(expectationManager))
|
||||
.withMessageContaining("ExpectationManager must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWhenExpectationManagerSupplierIsNullShouldThrowException() {
|
||||
Supplier<? extends RequestExpectationManager> expectationManagerSupplier = null;
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> new MockServerRestClientCustomizer(expectationManagerSupplier))
|
||||
.withMessageContaining("ExpectationManagerSupplier must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createShouldUseExpectationManagerClass() {
|
||||
MockServerRestClientCustomizer customizer = new MockServerRestClientCustomizer(
|
||||
UnorderedRequestExpectationManager.class);
|
||||
customizer.customize(RestClient.builder());
|
||||
assertThat(customizer.getServer()).extracting("expectationManager")
|
||||
.isInstanceOf(UnorderedRequestExpectationManager.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createShouldUseSupplier() {
|
||||
MockServerRestClientCustomizer customizer = new MockServerRestClientCustomizer(
|
||||
UnorderedRequestExpectationManager::new);
|
||||
customizer.customize(RestClient.builder());
|
||||
assertThat(customizer.getServer()).extracting("expectationManager")
|
||||
.isInstanceOf(UnorderedRequestExpectationManager.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void customizeShouldBindServer() {
|
||||
Builder builder = RestClient.builder();
|
||||
this.customizer.customize(builder);
|
||||
this.customizer.getServer().expect(requestTo("/test")).andRespond(withSuccess());
|
||||
builder.build().get().uri("/test").retrieve().toEntity(String.class);
|
||||
this.customizer.getServer().verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getServerWhenNoServersAreBoundShouldThrowException() {
|
||||
assertThatIllegalStateException().isThrownBy(this.customizer::getServer)
|
||||
.withMessageContaining("Unable to return a single MockRestServiceServer since "
|
||||
+ "MockServerRestClientCustomizer has not been bound to a RestClient");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getServerWhenMultipleServersAreBoundShouldThrowException() {
|
||||
this.customizer.customize(RestClient.builder());
|
||||
this.customizer.customize(RestClient.builder());
|
||||
assertThatIllegalStateException().isThrownBy(this.customizer::getServer)
|
||||
.withMessageContaining("Unable to return a single MockRestServiceServer since "
|
||||
+ "MockServerRestClientCustomizer has been bound to more than one RestClient");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getServerWhenSingleServerIsBoundShouldReturnServer() {
|
||||
Builder builder = RestClient.builder();
|
||||
this.customizer.customize(builder);
|
||||
assertThat(this.customizer.getServer()).isEqualTo(this.customizer.getServer(builder));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getServerWhenRestClientBuilderIsFoundShouldReturnServer() {
|
||||
Builder builder1 = RestClient.builder();
|
||||
Builder builder2 = RestClient.builder();
|
||||
this.customizer.customize(builder1);
|
||||
this.customizer.customize(builder2);
|
||||
assertThat(this.customizer.getServer(builder1)).isNotNull();
|
||||
assertThat(this.customizer.getServer(builder2)).isNotNull().isNotSameAs(this.customizer.getServer(builder1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getServerWhenRestClientBuilderIsNotFoundShouldReturnNull() {
|
||||
Builder builder1 = RestClient.builder();
|
||||
Builder builder2 = RestClient.builder();
|
||||
this.customizer.customize(builder1);
|
||||
assertThat(this.customizer.getServer(builder1)).isNotNull();
|
||||
assertThat(this.customizer.getServer(builder2)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getServersShouldReturnServers() {
|
||||
Builder builder1 = RestClient.builder();
|
||||
Builder builder2 = RestClient.builder();
|
||||
this.customizer.customize(builder1);
|
||||
this.customizer.customize(builder2);
|
||||
assertThat(this.customizer.getServers()).containsOnlyKeys(builder1, builder2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getExpectationManagersShouldReturnExpectationManagers() {
|
||||
Builder builder1 = RestClient.builder();
|
||||
Builder builder2 = RestClient.builder();
|
||||
this.customizer.customize(builder1);
|
||||
this.customizer.customize(builder2);
|
||||
RequestExpectationManager manager1 = this.customizer.getExpectationManagers().get(builder1);
|
||||
RequestExpectationManager manager2 = this.customizer.getExpectationManagers().get(builder2);
|
||||
assertThat(this.customizer.getServer(builder1)).extracting("expectationManager").isEqualTo(manager1);
|
||||
assertThat(this.customizer.getServer(builder2)).extracting("expectationManager").isEqualTo(manager2);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue