Skip to content

Commit a9847d3

Browse files
Copilotrashidi
andauthored
Migrate from TestRestTemplate to RestTestClient (#399)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: rashidi <380073+rashidi@users.noreply.github.com> Co-authored-by: Rashidi Zin <rashidi@zin.my>
1 parent c6ea3be commit a9847d3

11 files changed

Lines changed: 137 additions & 169 deletions

File tree

‎cloud-jdbc-env-repo/README.adoc‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,22 +169,24 @@ Next we will have link:{url-quickref}/src/test/java/zin/rashidi/boot/cloud/jdbce
169169

170170
[source,java]
171171
----
172+
@AutoConfigureRestTestClient
172173
@Testcontainers
173174
@SpringBootTest(properties = "spring.datasource.url=jdbc:tc:mysql:lts:///test?TC_INITSCRIPT=init-script.sql", webEnvironment = RANDOM_PORT)
174175
class CloudJdbcEnvRepoApplicationTests {
175176
176177
@Container
177-
private static final MySQLContainer<?> MYSQL = new MySQLContainer<>("mysql:lts");
178+
private static final MySQLContainer MYSQL = new MySQLContainer("mysql:lts");
178179
179180
@Autowired
180-
private TestRestTemplate restClient;
181+
private RestTestClient restClient;
181182
182183
@Test
183184
@DisplayName("Given app.greet.name is configured to Demo in the database When I call greet Then I should get Hello, my name is Demo")
184185
void greet() {
185-
var response = restClient.getForEntity("/greet?greeting={0}", String.class, "Hello");
186-
187-
assertThat(response.getBody()).isEqualTo("Hello, my name is Demo");
186+
restClient.get().uri("/greet?greeting={greeting}", "Hello")
187+
.exchange()
188+
.expectStatus().isOk()
189+
.expectBody().jsonPath("$").isEqualTo("Hello, my name is Demo");
188190
}
189191
190192
}

‎cloud-jdbc-env-repo/src/test/java/zin/rashidi/boot/cloud/jdbcenvrepo/CloudJdbcEnvRepoApplicationTests.java‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
import org.junit.jupiter.api.DisplayName;
44
import org.junit.jupiter.api.Test;
55
import org.springframework.beans.factory.annotation.Autowired;
6-
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate;
6+
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureRestTestClient;
77
import org.springframework.boot.test.context.SpringBootTest;
8-
import org.springframework.boot.resttestclient.TestRestTemplate;
8+
import org.springframework.test.web.servlet.client.RestTestClient;
99
import org.testcontainers.junit.jupiter.Container;
1010
import org.testcontainers.junit.jupiter.Testcontainers;
1111
import org.testcontainers.mysql.MySQLContainer;
1212

13-
import static org.assertj.core.api.Assertions.assertThat;
1413
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
1514

16-
@AutoConfigureTestRestTemplate
15+
@AutoConfigureRestTestClient
1716
@Testcontainers
1817
@SpringBootTest(properties = "spring.datasource.url=jdbc:tc:mysql:lts:///test?TC_INITSCRIPT=init-script.sql", webEnvironment = RANDOM_PORT)
1918
class CloudJdbcEnvRepoApplicationTests {
@@ -22,14 +21,15 @@ class CloudJdbcEnvRepoApplicationTests {
2221
private static final MySQLContainer MYSQL = new MySQLContainer("mysql:lts");
2322

2423
@Autowired
25-
private TestRestTemplate restClient;
24+
private RestTestClient restClient;
2625

2726
@Test
2827
@DisplayName("Given app.greet.name is configured to Demo in the database When I call greet Then I should get Hello, my name is Demo")
2928
void greet() {
30-
var response = restClient.getForEntity("/greet?greeting={0}", String.class, "Hello");
31-
32-
assertThat(response.getBody()).isEqualTo("Hello, my name is Demo");
29+
restClient.get().uri("/greet?greeting={greeting}", "Hello")
30+
.exchange()
31+
.expectStatus().isOk()
32+
.expectBody().jsonPath("$").isEqualTo("Hello, my name is Demo");
3333
}
3434

3535
}

‎data-domain-events/README.adoc‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ In the spirit of TDD, we will start by implementing an integration end-to-end te
2525

2626
[source,java]
2727
----
28+
@AutoConfigureRestTestClient
2829
@SpringBootTest(
2930
classes = TestDataDomainEventsApplication.class,
3031
properties = "spring.jpa.hibernate.ddl-auto=create",
@@ -39,7 +40,7 @@ class BookPurchaseTests {
3940
private BookRepository books;
4041
4142
@Autowired
42-
private TestRestTemplate client;
43+
private RestTestClient client;
4344
4445
private Book book;
4546
@@ -53,7 +54,9 @@ class BookPurchaseTests {
5354
@Test
5455
@DisplayName("Given total book availability is 100 When a book is purchased Then total book availability should be 99")
5556
void purchase() {
56-
client.delete("/books/{id}/purchase", book.getId());
57+
client.delete().uri("/books/{id}/purchase", book.getId())
58+
.exchange()
59+
.expectStatus().isOk();
5760
5861
var availability = availabilities.findByIsbn(book.getIsbn());
5962

‎data-domain-events/src/test/java/zin/rashidi/boot/data/de/availability/BookPurchaseTests.java‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
import org.junit.jupiter.api.BeforeEach;
77
import org.junit.jupiter.api.DisplayName;
88
import org.junit.jupiter.api.Test;
9-
import org.springframework.beans.factory.annotation.Autowired;
10-
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureRestTestClient;
1111
import org.springframework.boot.test.context.SpringBootTest;
12-
import org.springframework.boot.resttestclient.TestRestTemplate;
12+
import org.springframework.test.web.servlet.client.RestTestClient;
1313

1414
import zin.rashidi.boot.data.de.TestDataDomainEventsApplication;
1515
import zin.rashidi.boot.data.de.book.Book;
@@ -18,7 +18,7 @@
1818
/**
1919
* @author Rashidi Zin
2020
*/
21-
@AutoConfigureTestRestTemplate
21+
@AutoConfigureRestTestClient
2222
@SpringBootTest(
2323
classes = TestDataDomainEventsApplication.class,
2424
properties = "spring.jpa.hibernate.ddl-auto=create",
@@ -33,7 +33,7 @@ class BookPurchaseTests {
3333
private BookRepository books;
3434

3535
@Autowired
36-
private TestRestTemplate client;
36+
private RestTestClient client;
3737

3838
private Book book;
3939

@@ -47,7 +47,9 @@ void setup() {
4747
@Test
4848
@DisplayName("Given total book availability is 100 When a book is purchased Then total book availability should be 99")
4949
void purchase() {
50-
client.delete("/books/{id}/purchase", book.getId());
50+
client.delete().uri("/books/{id}/purchase", book.getId())
51+
.exchange()
52+
.expectStatus().isOk();
5153

5254
var availability = availabilities.findByIsbn(book.getIsbn());
5355

‎data-mongodb-transactional/README.adoc‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,34 @@ We will verify our implementation through integration test whereby we will creat
3232

3333
[source,java]
3434
----
35+
@AutoConfigureRestTestClient
3536
@SpringBootTest(webEnvironment = RANDOM_PORT)
3637
@Testcontainers
3738
class CreateUserTests {
3839
3940
@Container
4041
@ServiceConnection
41-
private static final MongoDBContainer mongo = new MongoDBContainer("mongo:latest");
42+
private static final MongoDBContainer mongo = new MongoDBContainer("mongo:latest").withReplicaSet();
4243
4344
@Autowired
44-
private TestRestTemplate restTemplate;
45+
private RestTestClient restTemplate;
4546
4647
@Test
4748
void create() {
48-
var headers = new HttpHeaders() {{ setContentType(APPLICATION_JSON); }};
4949
var body = """
5050
{
5151
"username": "rashidi.zin",
5252
"name": "Rashidi Zin"
5353
}
5454
""";
5555
56-
var response = restTemplate.exchange("/users", POST, new HttpEntity<>(body, headers), User.class);
57-
var createdUser = response.getBody();
58-
59-
assertThat(createdUser).extracting("status").isEqualTo(ACTIVE);
56+
restTemplate.post().uri("/users")
57+
.contentType(APPLICATION_JSON)
58+
.body(body)
59+
.exchange()
60+
.expectStatus().isCreated()
61+
.expectBody()
62+
.jsonPath("$.status").isEqualTo("ACTIVE");
6063
}
6164
6265
}
Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
package zin.rashidi.boot.data.mongodb.tm.user;
22

33
import org.junit.jupiter.api.Test;
4-
import org.springframework.beans.factory.annotation.Autowired;
5-
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureRestTestClient;
66
import org.springframework.boot.test.context.SpringBootTest;
7-
import org.springframework.boot.resttestclient.TestRestTemplate;
87
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
9-
import org.springframework.http.HttpEntity;
10-
import org.springframework.http.HttpHeaders;
8+
import org.springframework.test.web.servlet.client.RestTestClient;
119
import org.testcontainers.junit.jupiter.Container;
12-
import org.testcontainers.junit.jupiter.Testcontainers;
10+
import org.testcontainers.junit.jupiter.Testcontainers;
1311
import org.testcontainers.mongodb.MongoDBContainer;
1412

15-
import static org.assertj.core.api.Assertions.assertThat;
1613
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
17-
import static org.springframework.http.HttpMethod.POST;
1814
import static org.springframework.http.MediaType.APPLICATION_JSON;
1915
import static zin.rashidi.boot.data.mongodb.tm.user.User.Status.ACTIVE;
2016

2117
/**
2218
* @author Rashidi Zin
2319
*/
24-
@AutoConfigureTestRestTemplate
20+
@AutoConfigureRestTestClient
2521
@SpringBootTest(webEnvironment = RANDOM_PORT)
2622
@Testcontainers
2723
class CreateUserTests {
@@ -31,22 +27,24 @@ class CreateUserTests {
3127
private static final MongoDBContainer mongo = new MongoDBContainer("mongo:latest").withReplicaSet();
3228

3329
@Autowired
34-
private TestRestTemplate restTemplate;
30+
private RestTestClient restTemplate;
3531

3632
@Test
3733
void create() {
38-
var headers = new HttpHeaders() {{ setContentType(APPLICATION_JSON); }};
3934
var body = """
4035
{
4136
"username": "rashidi.zin",
4237
"name": "Rashidi Zin"
4338
}
4439
""";
4540

46-
var response = restTemplate.exchange("/users", POST, new HttpEntity<>(body, headers), User.class);
47-
var createdUser = response.getBody();
48-
49-
assertThat(createdUser).extracting("status").isEqualTo(ACTIVE);
41+
restTemplate.post().uri("/users")
42+
.contentType(APPLICATION_JSON)
43+
.body(body)
44+
.exchange()
45+
.expectStatus().isCreated()
46+
.expectBody()
47+
.jsonPath("$.status").isEqualTo("ACTIVE");
5048
}
5149

5250
}

‎data-rest-validation/README.adoc‎

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,13 @@ We will perform a `POST` request to create a `Book` with an `Author` that is `IN
8585

8686
[source,java]
8787
----
88+
@AutoConfigureRestTestClient
8889
@Import(TestDataRestValidationApplication.class)
8990
@SpringBootTest(webEnvironment = RANDOM_PORT, properties = "spring.jpa.hibernate.ddl-auto=create-drop")
9091
class CreateBookTests {
9192
9293
@Autowired
93-
private TestRestTemplate restClient;
94+
private RestTestClient restClient;
9495
9596
@Test
9697
@DisplayName("When I create a Book with an inactive Author, I should get a Bad Request response")
@@ -102,14 +103,13 @@ class CreateBookTests {
102103
}
103104
""".formatted(authorUri());
104105
105-
var response = restClient.exchange("/books", POST, new HttpEntity<>(body, headers()), RepositoryRestErrorResponse.class);
106-
107-
assertThat(response.getStatusCode()).isEqualTo(BAD_REQUEST);
108-
109-
assertThat(response.getBody().getErrors())
110-
.hasSize(1)
111-
.extracting(ValidationError::getMessage)
112-
.containsExactly("Author is inactive");
106+
var response = restClient.post().uri("/books")
107+
.headers(httpHeaders -> httpHeaders.addAll(headers()))
108+
.body(body)
109+
.exchange()
110+
.expectStatus().isBadRequest()
111+
.expectBody()
112+
.jsonPath("$.errors[0].message").isEqualTo("Author is inactive");
113113
}
114114
115115
private URI authorUri() {
@@ -120,11 +120,25 @@ class CreateBookTests {
120120
}
121121
""";
122122
123-
return restClient.exchange("/authors", POST, new HttpEntity<>(body, headers()), Void.class)
124-
.getHeaders()
123+
return restClient.post().uri("/authors")
124+
.headers(httpHeaders -> httpHeaders.addAll(headers()))
125+
.body(body)
126+
.exchange()
127+
.expectStatus().isCreated()
128+
.expectBody()
129+
.returnResult()
130+
.getResponseHeaders()
125131
.getLocation();
126132
}
127133
134+
private HttpHeaders headers() {
135+
var headers = new HttpHeaders();
136+
137+
headers.setContentType(APPLICATION_JSON);
138+
139+
return headers;
140+
}
141+
128142
}
129143
----
130144

0 commit comments

Comments
 (0)