From 68d9aa05a6f8f3ea1136926ae81b082e12bece1c Mon Sep 17 00:00:00 2001 From: Rashidi Zin Date: Wed, 9 Sep 2026 09:30:26 +0800 Subject: [PATCH 1/3] Replace TestRestTemplate with RestClient --- web-virtual-threads/README.adoc | 19 +++++++++------ .../user/UserControllerTests.java | 23 ++++++++++++------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/web-virtual-threads/README.adoc b/web-virtual-threads/README.adoc index 996f6628..78c23da6 100644 --- a/web-virtual-threads/README.adoc +++ b/web-virtual-threads/README.adoc @@ -49,7 +49,6 @@ Configure the application to enable virtual threads in `src/main/resources/appli spring.application.name=web-virtual-threads spring.threads.virtual.enabled=true -spring.jpa.hibernate.ddl-auto=create-drop ---- Setting `spring.threads.virtual.enabled=true` instructs Tomcat (or your chosen embedded web server) to use an `Executor` backed by virtual threads for handling HTTP requests. @@ -161,23 +160,29 @@ public class TestcontainersConfiguration { [source,java] ---- -@AutoConfigureTestRestTemplate @Import(TestcontainersConfiguration.class) @SpringBootTest(properties = "spring.jpa.hibernate.ddl-auto=create-drop", webEnvironment = RANDOM_PORT) class UserControllerTests { - @Autowired - private TestRestTemplate restClient; + @LocalServerPort + private int port; + + private RestClient restClient; @Autowired - private UserRepository em; + private UserRepository users; + + @BeforeEach + void setup(@Autowired RestClient.Builder restClientBuilder) { + restClient = restClientBuilder.baseUrl("http://localhost:" + port).build(); + } @Test @DisplayName("Should process web request and database query on a Virtual Thread") void virtualThreadEnabled() { - em.save(new User("Rashidi")); + users.save(new User("Rashidi")); - var response = restClient.getForEntity("/thread-info", Map.class); + var response = restClient.get().uri("/thread-info").retrieve().toEntity(Map.class); assertThat(response) .extracting("statusCode", "body.isVirtual", "body.userCount") diff --git a/web-virtual-threads/src/test/java/zin/rashidi/boot/web/virtualthreads/user/UserControllerTests.java b/web-virtual-threads/src/test/java/zin/rashidi/boot/web/virtualthreads/user/UserControllerTests.java index 971082a1..fc809f08 100644 --- a/web-virtual-threads/src/test/java/zin/rashidi/boot/web/virtualthreads/user/UserControllerTests.java +++ b/web-virtual-threads/src/test/java/zin/rashidi/boot/web/virtualthreads/user/UserControllerTests.java @@ -1,12 +1,13 @@ package zin.rashidi.boot.web.virtualthreads.user; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.resttestclient.TestRestTemplate; -import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.context.annotation.Import; +import org.springframework.web.client.RestClient; import zin.rashidi.boot.web.virtualthreads.TestcontainersConfiguration; import java.util.Map; @@ -15,23 +16,29 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; import static org.springframework.http.HttpStatus.OK; -@AutoConfigureTestRestTemplate @Import(TestcontainersConfiguration.class) @SpringBootTest(properties = "spring.jpa.hibernate.ddl-auto=create-drop", webEnvironment = RANDOM_PORT) class UserControllerTests { - @Autowired - private TestRestTemplate restClient; + @LocalServerPort + private int port; + + private RestClient restClient; @Autowired - private UserRepository em; + private UserRepository users; + + @BeforeEach + void setup(@Autowired RestClient.Builder restClientBuilder) { + restClient = restClientBuilder.baseUrl("http://localhost:" + port).build(); + } @Test @DisplayName("Should process web request and database query on a Virtual Thread") void virtualThreadEnabled() { - em.save(new User("Rashidi")); + users.save(new User("Rashidi")); - var response = restClient.getForEntity("/thread-info", Map.class); + var response = restClient.get().uri("/thread-info").retrieve().toEntity(Map.class); assertThat(response) .extracting("statusCode", "body.isVirtual", "body.userCount") From e5e8501d0019cef49c7dba21cc0051e087748f91 Mon Sep 17 00:00:00 2001 From: Rashidi Zin Date: Wed, 9 Sep 2026 09:56:50 +0800 Subject: [PATCH 2/3] Replace TestRestTemplate with RestTestClient --- web-virtual-threads/README.adoc | 20 ++++++-------- web-virtual-threads/build.gradle.kts | 1 - .../user/UserControllerTests.java | 27 +++++++------------ 3 files changed, 18 insertions(+), 30 deletions(-) diff --git a/web-virtual-threads/README.adoc b/web-virtual-threads/README.adoc index 78c23da6..78fc4051 100644 --- a/web-virtual-threads/README.adoc +++ b/web-virtual-threads/README.adoc @@ -29,7 +29,6 @@ We include standard Spring Web, Spring Data JPA, alongside PostgreSQL and Testco dependencies { implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation("org.springframework.boot:spring-boot-starter-webmvc") - implementation("org.springframework.boot:spring-boot-restclient") runtimeOnly("org.postgresql:postgresql") testImplementation("org.springframework.boot:spring-boot-starter-data-jpa-test") testImplementation("org.springframework.boot:spring-boot-resttestclient") @@ -160,6 +159,7 @@ public class TestcontainersConfiguration { [source,java] ---- +@AutoConfigureRestTestClient @Import(TestcontainersConfiguration.class) @SpringBootTest(properties = "spring.jpa.hibernate.ddl-auto=create-drop", webEnvironment = RANDOM_PORT) class UserControllerTests { @@ -167,26 +167,22 @@ class UserControllerTests { @LocalServerPort private int port; - private RestClient restClient; + @Autowired + private RestTestClient restClient; @Autowired private UserRepository users; - @BeforeEach - void setup(@Autowired RestClient.Builder restClientBuilder) { - restClient = restClientBuilder.baseUrl("http://localhost:" + port).build(); - } - @Test @DisplayName("Should process web request and database query on a Virtual Thread") void virtualThreadEnabled() { users.save(new User("Rashidi")); - var response = restClient.get().uri("/thread-info").retrieve().toEntity(Map.class); - - assertThat(response) - .extracting("statusCode", "body.isVirtual", "body.userCount") - .containsOnly(OK, true, 1); + restClient.get().uri("/thread-info").exchange() + .expectStatus().isOk() + .expectBody() + .jsonPath("$.isVirtual").isEqualTo(true) + .jsonPath("$.userCount").isEqualTo(1); } } diff --git a/web-virtual-threads/build.gradle.kts b/web-virtual-threads/build.gradle.kts index aceb56d5..24a2b676 100644 --- a/web-virtual-threads/build.gradle.kts +++ b/web-virtual-threads/build.gradle.kts @@ -20,7 +20,6 @@ repositories { dependencies { implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation("org.springframework.boot:spring-boot-starter-webmvc") - implementation("org.springframework.boot:spring-boot-restclient") runtimeOnly("org.postgresql:postgresql") testImplementation("org.springframework.boot:spring-boot-starter-data-jpa-test") testImplementation("org.springframework.boot:spring-boot-resttestclient") diff --git a/web-virtual-threads/src/test/java/zin/rashidi/boot/web/virtualthreads/user/UserControllerTests.java b/web-virtual-threads/src/test/java/zin/rashidi/boot/web/virtualthreads/user/UserControllerTests.java index fc809f08..6b53c427 100644 --- a/web-virtual-threads/src/test/java/zin/rashidi/boot/web/virtualthreads/user/UserControllerTests.java +++ b/web-virtual-threads/src/test/java/zin/rashidi/boot/web/virtualthreads/user/UserControllerTests.java @@ -1,21 +1,18 @@ package zin.rashidi.boot.web.virtualthreads.user; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureRestTestClient; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.context.annotation.Import; -import org.springframework.web.client.RestClient; +import org.springframework.test.web.servlet.client.RestTestClient; import zin.rashidi.boot.web.virtualthreads.TestcontainersConfiguration; -import java.util.Map; - -import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; -import static org.springframework.http.HttpStatus.OK; +@AutoConfigureRestTestClient @Import(TestcontainersConfiguration.class) @SpringBootTest(properties = "spring.jpa.hibernate.ddl-auto=create-drop", webEnvironment = RANDOM_PORT) class UserControllerTests { @@ -23,26 +20,22 @@ class UserControllerTests { @LocalServerPort private int port; - private RestClient restClient; + @Autowired + private RestTestClient restClient; @Autowired private UserRepository users; - @BeforeEach - void setup(@Autowired RestClient.Builder restClientBuilder) { - restClient = restClientBuilder.baseUrl("http://localhost:" + port).build(); - } - @Test @DisplayName("Should process web request and database query on a Virtual Thread") void virtualThreadEnabled() { users.save(new User("Rashidi")); - var response = restClient.get().uri("/thread-info").retrieve().toEntity(Map.class); - - assertThat(response) - .extracting("statusCode", "body.isVirtual", "body.userCount") - .containsOnly(OK, true, 1); + restClient.get().uri("/thread-info").exchange() + .expectStatus().isOk() + .expectBody() + .jsonPath("$.isVirtual").isEqualTo(true) + .jsonPath("$.userCount").isEqualTo(1); } } From b3ce920eedb7d42d461c1ef2ed8949973f2377c4 Mon Sep 17 00:00:00 2001 From: Rashidi Zin Date: Wed, 9 Sep 2026 09:58:07 +0800 Subject: [PATCH 3/3] Replace TestRestTemplate with RestTestClient --- web-virtual-threads/README.adoc | 3 --- .../boot/web/virtualthreads/user/UserControllerTests.java | 4 ---- 2 files changed, 7 deletions(-) diff --git a/web-virtual-threads/README.adoc b/web-virtual-threads/README.adoc index 78fc4051..32f69db4 100644 --- a/web-virtual-threads/README.adoc +++ b/web-virtual-threads/README.adoc @@ -164,9 +164,6 @@ public class TestcontainersConfiguration { @SpringBootTest(properties = "spring.jpa.hibernate.ddl-auto=create-drop", webEnvironment = RANDOM_PORT) class UserControllerTests { - @LocalServerPort - private int port; - @Autowired private RestTestClient restClient; diff --git a/web-virtual-threads/src/test/java/zin/rashidi/boot/web/virtualthreads/user/UserControllerTests.java b/web-virtual-threads/src/test/java/zin/rashidi/boot/web/virtualthreads/user/UserControllerTests.java index 6b53c427..3f98026d 100644 --- a/web-virtual-threads/src/test/java/zin/rashidi/boot/web/virtualthreads/user/UserControllerTests.java +++ b/web-virtual-threads/src/test/java/zin/rashidi/boot/web/virtualthreads/user/UserControllerTests.java @@ -5,7 +5,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureRestTestClient; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.context.annotation.Import; import org.springframework.test.web.servlet.client.RestTestClient; import zin.rashidi.boot.web.virtualthreads.TestcontainersConfiguration; @@ -17,9 +16,6 @@ @SpringBootTest(properties = "spring.jpa.hibernate.ddl-auto=create-drop", webEnvironment = RANDOM_PORT) class UserControllerTests { - @LocalServerPort - private int port; - @Autowired private RestTestClient restClient;