diff --git a/web-virtual-threads/README.adoc b/web-virtual-threads/README.adoc index 996f6628..32f69db4 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") @@ -49,7 +48,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,27 +159,27 @@ public class TestcontainersConfiguration { [source,java] ---- -@AutoConfigureTestRestTemplate +@AutoConfigureRestTestClient @Import(TestcontainersConfiguration.class) @SpringBootTest(properties = "spring.jpa.hibernate.ddl-auto=create-drop", webEnvironment = RANDOM_PORT) class UserControllerTests { @Autowired - private TestRestTemplate restClient; + private RestTestClient restClient; @Autowired - private UserRepository em; + private UserRepository users; @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); - - 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 971082a1..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 @@ -3,39 +3,35 @@ 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.resttestclient.autoconfigure.AutoConfigureRestTestClient; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Import; +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; -@AutoConfigureTestRestTemplate +@AutoConfigureRestTestClient @Import(TestcontainersConfiguration.class) @SpringBootTest(properties = "spring.jpa.hibernate.ddl-auto=create-drop", webEnvironment = RANDOM_PORT) class UserControllerTests { @Autowired - private TestRestTemplate restClient; + private RestTestClient restClient; @Autowired - private UserRepository em; + private UserRepository users; @Test @DisplayName("Should process web request and database query on a Virtual Thread") void virtualThreadEnabled() { - em.save(new User("Rashidi")); - - var response = restClient.getForEntity("/thread-info", Map.class); + users.save(new User("Rashidi")); - 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); } }