Use RestClient in integration tests

Now that `RestClient` offers a better alternative, this commit revisits
our integration tests to use `RestClient` instead of `RestTemplate`.

Closes gh-36573
This commit is contained in:
Brian Clozel
2026-03-31 11:37:43 +02:00
parent f850b051b5
commit 9c4b246c8c
37 changed files with 361 additions and 485 deletions
@@ -30,7 +30,7 @@ import org.springframework.mock.http.client.MockClientHttpRequest;
import org.springframework.mock.http.client.MockClientHttpResponse;
import org.springframework.test.web.client.MockRestServiceServer.MockRestServiceServerBuilder;
import org.springframework.test.web.client.response.ExecutingResponseCreator;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestClient;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -49,65 +49,72 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
*/
class MockRestServiceServerTests {
private final RestTemplate restTemplate = new RestTemplate();
private final RestClient.Builder clientBuilder = RestClient.builder();
@Test
void buildMultipleTimes() {
MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(this.restTemplate);
MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(this.clientBuilder);
MockRestServiceServer server = builder.build();
RestClient restClient = this.clientBuilder.build();
server.expect(requestTo("/foo")).andRespond(withSuccess());
this.restTemplate.getForObject("/foo", Void.class);
restClient.get().uri("/foo").retrieve().toBodilessEntity();
server.verify();
server = builder.ignoreExpectOrder(true).build();
restClient = this.clientBuilder.build();
server.expect(requestTo("/foo")).andRespond(withSuccess());
server.expect(requestTo("/bar")).andRespond(withSuccess());
this.restTemplate.getForObject("/bar", Void.class);
this.restTemplate.getForObject("/foo", Void.class);
restClient.get().uri("/bar").retrieve().toBodilessEntity();
restClient.get().uri("/foo").retrieve().toBodilessEntity();
server.verify();
server = builder.build();
restClient = this.clientBuilder.build();
server.expect(requestTo("/bar")).andRespond(withSuccess());
this.restTemplate.getForObject("/bar", Void.class);
restClient.get().uri("/bar").retrieve().toBodilessEntity();
server.verify();
}
@Test
void exactExpectOrder() {
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate)
MockRestServiceServer server = MockRestServiceServer.bindTo(this.clientBuilder)
.ignoreExpectOrder(false).build();
RestClient restClient = this.clientBuilder.build();
server.expect(requestTo("/foo")).andRespond(withSuccess());
server.expect(requestTo("/bar")).andRespond(withSuccess());
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
this.restTemplate.getForObject("/bar", Void.class));
restClient.get().uri("/bar").retrieve().toBodilessEntity());
}
@Test
void ignoreExpectOrder() {
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate)
MockRestServiceServer server = MockRestServiceServer.bindTo(this.clientBuilder)
.ignoreExpectOrder(true).build();
RestClient restClient = this.clientBuilder.build();
server.expect(requestTo("/foo")).andRespond(withSuccess());
server.expect(requestTo("/bar")).andRespond(withSuccess());
this.restTemplate.getForObject("/bar", Void.class);
this.restTemplate.getForObject("/foo", Void.class);
restClient.get().uri("/bar").retrieve().toBodilessEntity();
restClient.get().uri("/foo").retrieve().toBodilessEntity();
server.verify();
}
@Test
void executingResponseCreator() {
RestTemplate restTemplate = createEchoRestTemplate();
ExecutingResponseCreator withActualCall = new ExecutingResponseCreator(restTemplate.getRequestFactory());
ClientHttpRequestFactory clientRequestFactory = createEchoClientRequestFactory();
ExecutingResponseCreator withActualCall = new ExecutingResponseCreator(clientRequestFactory);
this.clientBuilder.requestFactory(clientRequestFactory);
MockRestServiceServer server = MockRestServiceServer.bindTo(this.clientBuilder).build();
RestClient restClient = this.clientBuilder.build();
MockRestServiceServer server = MockRestServiceServer.bindTo(restTemplate).build();
server.expect(requestTo("/profile")).andRespond(withSuccess());
server.expect(requestTo("/quoteOfTheDay")).andRespond(withActualCall);
var response1 = restTemplate.getForEntity("/profile", String.class);
var response2 = restTemplate.getForEntity("/quoteOfTheDay", String.class);
var response1 = restClient.get().uri("/profile").retrieve().toEntity(String.class);
var response2 = restClient.get().uri("/quoteOfTheDay").retrieve().toEntity(String.class);
server.verify();
assertThat(response1.getStatusCode().value()).isEqualTo(200);
@@ -116,8 +123,8 @@ class MockRestServiceServerTests {
assertThat(response2.getBody()).isEqualTo("echo from /quoteOfTheDay");
}
private static RestTemplate createEchoRestTemplate() {
ClientHttpRequestFactory requestFactory = (uri, httpMethod) -> {
private static ClientHttpRequestFactory createEchoClientRequestFactory() {
return (uri, httpMethod) -> {
MockClientHttpRequest request = new MockClientHttpRequest(httpMethod, uri);
ClientHttpResponse response = new MockClientHttpResponse(
("echo from " + uri.getPath()).getBytes(StandardCharsets.UTF_8),
@@ -126,61 +133,64 @@ class MockRestServiceServerTests {
request.setResponse(response);
return request;
};
return new RestTemplate(requestFactory);
}
@Test
void resetAndReuseServer() {
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate).build();
MockRestServiceServer server = MockRestServiceServer.bindTo(this.clientBuilder).build();
RestClient restClient = this.clientBuilder.build();
server.expect(requestTo("/foo")).andRespond(withSuccess());
this.restTemplate.getForObject("/foo", Void.class);
restClient.get().uri("/foo").retrieve().toBodilessEntity();
server.verify();
server.reset();
server.expect(requestTo("/bar")).andRespond(withSuccess());
this.restTemplate.getForObject("/bar", Void.class);
restClient.get().uri("/bar").retrieve().toBodilessEntity();
server.verify();
}
@Test
void resetAndReuseServerWithUnorderedExpectationManager() {
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate)
MockRestServiceServer server = MockRestServiceServer.bindTo(this.clientBuilder)
.ignoreExpectOrder(true).build();
RestClient restClient = this.clientBuilder.build();
server.expect(requestTo("/foo")).andRespond(withSuccess());
this.restTemplate.getForObject("/foo", Void.class);
restClient.get().uri("/foo").retrieve().toBodilessEntity();
server.verify();
server.reset();
server.expect(requestTo("/foo")).andRespond(withSuccess());
server.expect(requestTo("/bar")).andRespond(withSuccess());
this.restTemplate.getForObject("/bar", Void.class);
this.restTemplate.getForObject("/foo", Void.class);
restClient.get().uri("/bar").retrieve().toBodilessEntity();
restClient.get().uri("/foo").retrieve().toBodilessEntity();
server.verify();
}
@Test // gh-24486
void resetClearsRequestFailures() {
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate).build();
MockRestServiceServer server = MockRestServiceServer.bindTo(this.clientBuilder).build();
RestClient restClient = this.clientBuilder.build();
server.expect(once(), requestTo("/remoteurl")).andRespond(withSuccess());
this.restTemplate.postForEntity("/remoteurl", null, String.class);
restClient.post().uri("/remoteurl").retrieve().body(String.class);
server.verify();
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> this.restTemplate.postForEntity("/remoteurl", null, String.class))
.isThrownBy(() -> restClient.post().uri("/remoteurl").retrieve().body(String.class))
.withMessageStartingWith("No further requests expected");
server.reset();
server.expect(once(), requestTo("/remoteurl")).andRespond(withSuccess());
this.restTemplate.postForEntity("/remoteurl", null, String.class);
restClient.post().uri("/remoteurl").retrieve().body(String.class);
server.verify();
}
@Test // SPR-16132
void followUpRequestAfterFailure() {
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate).build();
MockRestServiceServer server = MockRestServiceServer.bindTo(this.clientBuilder).build();
RestClient restClient = this.clientBuilder.build();
server.expect(requestTo("/some-service/some-endpoint"))
.andRespond(request -> { throw new SocketException("pseudo network error"); });
@@ -189,11 +199,11 @@ class MockRestServiceServerTests {
.andExpect(method(POST)).andRespond(withSuccess());
try {
this.restTemplate.getForEntity("/some-service/some-endpoint", String.class);
restClient.get().uri("/some-service/some-endpoint").retrieve().body(String.class);
fail("Expected exception");
}
catch (Exception ex) {
this.restTemplate.postForEntity("/reporting-service/report-error", ex.toString(), String.class);
restClient.post().uri("/reporting-service/report-error").retrieve().body(String.class);
}
server.verify();
@@ -201,12 +211,13 @@ class MockRestServiceServerTests {
@Test // gh-21799
void verifyShouldFailIfRequestsFailed() {
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate).build();
MockRestServiceServer server = MockRestServiceServer.bindTo(this.clientBuilder).build();
server.expect(once(), requestTo("/remoteurl")).andRespond(withSuccess());
RestClient restClient = this.clientBuilder.build();
this.restTemplate.postForEntity("/remoteurl", null, String.class);
restClient.post().uri("/remoteurl").retrieve().body(String.class);
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> this.restTemplate.postForEntity("/remoteurl", null, String.class))
.isThrownBy(() -> restClient.post().uri("/remoteurl").retrieve().body(String.class))
.withMessageStartingWith("No further requests expected");
assertThatExceptionOfType(AssertionError.class)
@@ -216,24 +227,26 @@ class MockRestServiceServerTests {
@Test
void verifyWithTimeout() {
MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(this.restTemplate);
MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(this.clientBuilder);
MockRestServiceServer server1 = builder.build();
RestClient restClient = this.clientBuilder.build();
server1.expect(requestTo("/foo")).andRespond(withSuccess());
server1.expect(requestTo("/bar")).andRespond(withSuccess());
this.restTemplate.getForObject("/foo", Void.class);
restClient.get().uri("/foo").retrieve().toBodilessEntity();
assertThatThrownBy(() -> server1.verify(Duration.ofMillis(100))).hasMessage("""
Further request(s) expected leaving 1 unsatisfied expectation(s).
1 request(s) executed:
GET /foo, headers: [Accept:"application/json, application/*+json"]
GET /foo
""");
MockRestServiceServer server2 = builder.build();
restClient = this.clientBuilder.build();
server2.expect(requestTo("/foo")).andRespond(withSuccess());
server2.expect(requestTo("/bar")).andRespond(withSuccess());
this.restTemplate.getForObject("/foo", Void.class);
this.restTemplate.getForObject("/bar", Void.class);
restClient.get().uri("/foo").retrieve().toBodilessEntity();
restClient.get().uri("/bar").retrieve().toBodilessEntity();
server2.verify(Duration.ofMillis(100));
}
@@ -17,8 +17,8 @@
package org.springframework.test.web.client.samples;
import java.io.IOException;
import java.util.Collections;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
@@ -32,7 +32,7 @@ import org.springframework.http.client.ClientHttpResponse;
import org.springframework.test.web.Person;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestClient;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -46,17 +46,24 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
/**
* Examples to demonstrate writing client-side REST tests with Spring MVC Test.
*
* <p>While the tests in this class invoke the RestTemplate directly, in actual
* tests the RestTemplate may likely be invoked indirectly, i.e. through client
* <p>While the tests in this class invoke the RestClient directly, in actual
* tests the RestClient may likely be invoked indirectly, i.e. through client
* code.
*
* @author Rossen Stoyanchev
*/
class SampleTests {
private final RestTemplate restTemplate = new RestTemplate();
private RestClient restClient;
private final MockRestServiceServer mockServer = MockRestServiceServer.bindTo(this.restTemplate).ignoreExpectOrder(true).build();
private MockRestServiceServer mockServer;
@BeforeEach
void setup() {
RestClient.Builder clientBuilder = RestClient.builder();
this.mockServer = MockRestServiceServer.bindTo(clientBuilder).ignoreExpectOrder(true).build();
this.restClient = clientBuilder.build();
}
@Test
@@ -67,7 +74,7 @@ class SampleTests {
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
@SuppressWarnings("unused")
Person ludwig = this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
Person ludwig = this.restClient.get().uri("/composers/{id}", 42).retrieve().body(Person.class);
// We are only validating the request. The response is mocked out.
// hotel.getId() == 42
@@ -84,15 +91,15 @@ class SampleTests {
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
@SuppressWarnings("unused")
Person ludwig = this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
Person ludwig = this.restClient.get().uri("/composers/{id}", 42).retrieve().body(Person.class);
// We are only validating the request. The response is mocked out.
// hotel.getId() == 42
// hotel.getName().equals("Holiday Inn")
this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
this.restClient.get().uri("/composers/{id}", 42).retrieve().body(Person.class);
this.restClient.get().uri("/composers/{id}", 42).retrieve().body(Person.class);
this.restClient.get().uri("/composers/{id}", 42).retrieve().body(Person.class);
this.mockServer.verify();
}
@@ -106,7 +113,7 @@ class SampleTests {
this.mockServer.expect(never(), requestTo("/composers/43")).andExpect(method(HttpMethod.GET))
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
this.restClient.get().uri("/composers/{id}", 42).retrieve().body(Person.class);
this.mockServer.verify();
}
@@ -120,9 +127,9 @@ class SampleTests {
this.mockServer.expect(never(), requestTo("/composers/43")).andExpect(method(HttpMethod.GET))
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
this.restClient.get().uri("/composers/{id}", 42).retrieve().body(Person.class);
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
this.restTemplate.getForObject("/composers/{id}", Person.class, 43));
this.restClient.get().uri("/composers/{id}", 43).retrieve().body(Person.class));
}
@Test
@@ -133,7 +140,7 @@ class SampleTests {
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
@SuppressWarnings("unused")
Person ludwig = this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
Person ludwig = this.restClient.get().uri("/composers/{id}", 42).retrieve().body(Person.class);
// hotel.getId() == 42
// hotel.getName().equals("Holiday Inn")
@@ -156,11 +163,11 @@ class SampleTests {
.andRespond(withSuccess("8", MediaType.TEXT_PLAIN));
@SuppressWarnings("unused")
String result1 = this.restTemplate.getForObject("/number", String.class);
String result1 = this.restClient.get().uri("/number").retrieve().body(String.class);
// result1 == "1"
@SuppressWarnings("unused")
String result2 = this.restTemplate.getForObject("/number", String.class);
String result2 = this.restClient.get().uri("/number").retrieve().body(String.class);
// result == "2"
try {
@@ -175,18 +182,17 @@ class SampleTests {
void repeatedAccessToResponseViaResource() {
Resource resource = new ClassPathResource("ludwig.json", getClass());
RestTemplate restTemplate = new RestTemplate();
restTemplate.setInterceptors(Collections.singletonList(new ContentInterceptor(resource)));
MockRestServiceServer mockServer = MockRestServiceServer.bindTo(restTemplate)
RestClient.Builder clientBuilder = RestClient.builder().requestInterceptor(new ContentInterceptor(resource));
MockRestServiceServer mockServer = MockRestServiceServer.bindTo(clientBuilder)
.ignoreExpectOrder(true)
.bufferContent() // enable repeated reads of response body
.build();
RestClient restClient = clientBuilder.build();
mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
.andRespond(withSuccess(resource, MediaType.APPLICATION_JSON));
restTemplate.getForObject("/composers/{id}", Person.class, 42);
restClient.get().uri("/composers/{id}", 42).retrieve().body(Person.class);
mockServer.verify();
}
@@ -16,17 +16,12 @@
package org.springframework.test.web.client.samples.matchers;
import java.net.URI;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter;
import org.springframework.test.web.Person;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestClient;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.hamcrest.Matchers.startsWith;
@@ -44,15 +39,16 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
*/
class ContentRequestMatchersIntegrationTests {
private final RestTemplate restTemplate = new RestTemplate();
private RestClient restClient;
private final MockRestServiceServer mockServer = MockRestServiceServer.createServer(this.restTemplate);
private MockRestServiceServer mockServer;
@BeforeEach
void setup() {
this.restTemplate.setMessageConverters(
List.of(new StringHttpMessageConverter(), new JacksonJsonHttpMessageConverter()));
RestClient.Builder clientBuilder = RestClient.builder();
this.mockServer = MockRestServiceServer.createServer(clientBuilder);
this.restClient = clientBuilder.build();
}
@@ -89,7 +85,7 @@ class ContentRequestMatchersIntegrationTests {
}
private void executeAndVerify(Object body) {
this.restTemplate.put(URI.create("/foo"), body);
this.restClient.put().uri("/foo").body(body).retrieve().toBodilessEntity();
this.mockServer.verify();
}
@@ -16,18 +16,13 @@
package org.springframework.test.web.client.samples.matchers;
import java.net.URI;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter;
import org.springframework.test.web.Person;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestClient;
import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.header;
@@ -43,22 +38,23 @@ class HeaderRequestMatchersIntegrationTests {
private static final String RESPONSE_BODY = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";
private final RestTemplate restTemplate = new RestTemplate();
private RestClient restClient;
private final MockRestServiceServer mockServer = MockRestServiceServer.createServer(this.restTemplate);
private MockRestServiceServer mockServer;
@BeforeEach
void setup() {
this.restTemplate.setMessageConverters(
List.of(new StringHttpMessageConverter(), new JacksonJsonHttpMessageConverter()));
RestClient.Builder clientBuilder = RestClient.builder();
this.mockServer = MockRestServiceServer.createServer(clientBuilder);
this.restClient = clientBuilder.build();
}
@Test
void string() {
this.mockServer.expect(requestTo("/person/1"))
.andExpect(header("Accept", "application/json, application/*+json"))
.andExpect(header("Accept", "application/json"))
.andRespond(withSuccess(RESPONSE_BODY, MediaType.APPLICATION_JSON));
executeAndVerify();
@@ -74,7 +70,7 @@ class HeaderRequestMatchersIntegrationTests {
}
private void executeAndVerify() {
this.restTemplate.getForObject(URI.create("/person/1"), Person.class);
this.restClient.get().uri("/person/1").accept(MediaType.APPLICATION_JSON).retrieve().body(Person.class);
this.mockServer.verify();
}
@@ -16,18 +16,17 @@
package org.springframework.test.web.client.samples.matchers;
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter;
import org.springframework.http.MediaType;
import org.springframework.test.web.Person;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestClient;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;
@@ -64,10 +63,16 @@ class JsonPathRequestMatchersIntegrationTests {
}
private final RestTemplate restTemplate =
new RestTemplate(Collections.singletonList(new JacksonJsonHttpMessageConverter()));
private RestClient restClient;
private final MockRestServiceServer mockServer = MockRestServiceServer.createServer(this.restTemplate);
private MockRestServiceServer mockServer;
@BeforeEach
void setup() {
RestClient.Builder clientBuilder = RestClient.builder();
this.mockServer = MockRestServiceServer.createServer(clientBuilder);
this.restClient = clientBuilder.build();
}
@Test
@@ -179,7 +184,8 @@ class JsonPathRequestMatchersIntegrationTests {
}
private void executeAndVerify() {
this.restTemplate.put(URI.create("/composers"), people);
this.restClient.put().uri("/composers").contentType(MediaType.APPLICATION_JSON)
.body(people).retrieve().toBodilessEntity();
this.mockServer.verify();
}
@@ -16,8 +16,6 @@
package org.springframework.test.web.client.samples.matchers;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -29,11 +27,11 @@ import jakarta.xml.bind.annotation.XmlRootElement;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
import org.springframework.test.web.Person;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestClient;
import static org.hamcrest.Matchers.hasXPath;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.content;
@@ -61,7 +59,7 @@ class XmlContentRequestMatchersIntegrationTests {
private MockRestServiceServer mockServer;
private RestTemplate restTemplate;
private RestClient restClient;
private PeopleWrapper people;
@@ -76,13 +74,11 @@ class XmlContentRequestMatchersIntegrationTests {
this.people = new PeopleWrapper(composers);
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new Jaxb2RootElementHttpMessageConverter());
RestClient.Builder clientBuilder = RestClient.builder().configureMessageConverters(converters ->
converters.registerDefaults().withXmlConverter(new Jaxb2RootElementHttpMessageConverter()));
this.restTemplate = new RestTemplate();
this.restTemplate.setMessageConverters(converters);
this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
this.mockServer = MockRestServiceServer.createServer(clientBuilder);
this.restClient = clientBuilder.build();
}
@Test
@@ -106,7 +102,8 @@ class XmlContentRequestMatchersIntegrationTests {
}
private void executeAndVerify() {
this.restTemplate.put(URI.create("/composers"), this.people);
this.restClient.put().uri("/composers").contentType(MediaType.APPLICATION_XML)
.body(this.people).retrieve().toBodilessEntity();
this.mockServer.verify();
}
@@ -16,8 +16,6 @@
package org.springframework.test.web.client.samples.matchers;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -31,11 +29,11 @@ import jakarta.xml.bind.annotation.XmlRootElement;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
import org.springframework.test.web.Person;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestClient;
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.equalTo;
@@ -61,7 +59,7 @@ class XpathRequestMatchersIntegrationTests {
private MockRestServiceServer mockServer;
private RestTemplate restTemplate;
private RestClient restClient;
private PeopleWrapper people;
@@ -80,13 +78,11 @@ class XpathRequestMatchersIntegrationTests {
this.people = new PeopleWrapper(composers, performers);
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new Jaxb2RootElementHttpMessageConverter());
RestClient.Builder clientBuilder = RestClient.builder().configureMessageConverters(converters ->
converters.registerDefaults().withXmlConverter(new Jaxb2RootElementHttpMessageConverter()));
this.restTemplate = new RestTemplate();
this.restTemplate.setMessageConverters(converters);
this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
this.mockServer = MockRestServiceServer.createServer(clientBuilder);
this.restClient = clientBuilder.build();
}
@@ -190,7 +186,8 @@ class XpathRequestMatchersIntegrationTests {
}
private void executeAndVerify() {
this.restTemplate.put(URI.create("/composers"), this.people);
this.restClient.put().uri("/composers").contentType(MediaType.APPLICATION_XML)
.body(this.people).retrieve().toBodilessEntity();
this.mockServer.verify();
}