Skip to content

Commit dbfd038

Browse files
Validate combined RestTemplate and RestClient usage in mock REST config
Fixes gh-38820
1 parent f6fbd10 commit dbfd038

File tree

2 files changed

+90
-4
lines changed

2 files changed

+90
-4
lines changed

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/client/MockRestServiceServerAutoConfiguration.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -136,9 +136,12 @@ private RequestExpectationManager getDelegate() {
136136
.getExpectationManagers();
137137
Map<RestClient.Builder, RequestExpectationManager> restClientExpectationManagers = this.restClientCustomizer
138138
.getExpectationManagers();
139-
Assert.state(!(restTemplateExpectationManagers.isEmpty() && restClientExpectationManagers.isEmpty()),
140-
"Unable to use auto-configured MockRestServiceServer since "
141-
+ "a mock server customizer has not been bound to a RestTemplate or RestClient");
139+
boolean neitherBound = restTemplateExpectationManagers.isEmpty() && restClientExpectationManagers.isEmpty();
140+
boolean bothBound = !restTemplateExpectationManagers.isEmpty() && !restClientExpectationManagers.isEmpty();
141+
Assert.state(!neitherBound, "Unable to use auto-configured MockRestServiceServer since "
142+
+ "a mock server customizer has not been bound to a RestTemplate or RestClient");
143+
Assert.state(!bothBound, "Unable to use auto-configured MockRestServiceServer since "
144+
+ "mock server customizers have been bound to both a RestTemplate and a RestClient");
142145
if (!restTemplateExpectationManagers.isEmpty()) {
143146
Assert.state(restTemplateExpectationManagers.size() == 1,
144147
"Unable to use auto-configured MockRestServiceServer since "
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.web.client;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.test.web.client.MockServerRestClientCustomizer;
23+
import org.springframework.boot.test.web.client.MockServerRestTemplateCustomizer;
24+
import org.springframework.http.MediaType;
25+
import org.springframework.test.web.client.MockRestServiceServer;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
29+
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
30+
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
31+
32+
/**
33+
* Tests for {@link RestClientTest @RestClientTest} with a {@code RestTemplate} and a
34+
* {@code RestClient} clients.
35+
*
36+
* @author Scott Frederick
37+
*/
38+
@RestClientTest({ ExampleRestTemplateService.class, ExampleRestClientService.class })
39+
class RestClientTestRestTemplateAndRestClientTogetherIntegrationTests {
40+
41+
@Autowired
42+
private ExampleRestTemplateService restTemplateClient;
43+
44+
@Autowired
45+
private ExampleRestClientService restClientClient;
46+
47+
@Autowired
48+
private MockServerRestTemplateCustomizer templateCustomizer;
49+
50+
@Autowired
51+
private MockServerRestClientCustomizer clientCustomizer;
52+
53+
@Autowired
54+
private MockRestServiceServer server;
55+
56+
@Test
57+
void serverShouldNotWork() {
58+
assertThatIllegalStateException().isThrownBy(
59+
() -> this.server.expect(requestTo(uri("/test"))).andRespond(withSuccess("hello", MediaType.TEXT_HTML)))
60+
.withMessageContaining("Unable to use auto-configured");
61+
}
62+
63+
@Test
64+
void restTemplateClientRestCallViaCustomizer() {
65+
this.templateCustomizer.getServer()
66+
.expect(requestTo("/test"))
67+
.andRespond(withSuccess("hello", MediaType.TEXT_HTML));
68+
assertThat(this.restTemplateClient.test()).isEqualTo("hello");
69+
}
70+
71+
@Test
72+
void restClientClientRestCallViaCustomizer() {
73+
this.clientCustomizer.getServer()
74+
.expect(requestTo(uri("/test")))
75+
.andRespond(withSuccess("there", MediaType.TEXT_HTML));
76+
assertThat(this.restClientClient.test()).isEqualTo("there");
77+
}
78+
79+
private static String uri(String path) {
80+
return "https://example.com" + path;
81+
}
82+
83+
}

0 commit comments

Comments
 (0)