From 6df424bff4fe1dc87f1fca183181a402aa27e061 Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Sat, 30 Apr 2022 12:27:13 +0100 Subject: [PATCH] [JAVA-8145] Update and cleanup code --- .../com/baeldung/reactive/webclient/Foo.java | 24 ++--- .../webclient/WebClientApplication.java | 9 +- .../WebControllerIntegrationTest.java | 36 ++++---- .../WebTestClientIntegrationTest.java | 91 ++++++++----------- 4 files changed, 67 insertions(+), 93 deletions(-) diff --git a/spring-reactive/src/main/java/com/baeldung/reactive/webclient/Foo.java b/spring-reactive/src/main/java/com/baeldung/reactive/webclient/Foo.java index a58d672686..246ba30667 100644 --- a/spring-reactive/src/main/java/com/baeldung/reactive/webclient/Foo.java +++ b/spring-reactive/src/main/java/com/baeldung/reactive/webclient/Foo.java @@ -1,24 +1,14 @@ package com.baeldung.reactive.webclient; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor public class Foo { private String name; - public Foo() { - super(); - } - - public Foo(String name) { - super(); - this.name = name; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - } diff --git a/spring-reactive/src/main/java/com/baeldung/reactive/webclient/WebClientApplication.java b/spring-reactive/src/main/java/com/baeldung/reactive/webclient/WebClientApplication.java index fd1cb8aff1..3a62e7b8a5 100644 --- a/spring-reactive/src/main/java/com/baeldung/reactive/webclient/WebClientApplication.java +++ b/spring-reactive/src/main/java/com/baeldung/reactive/webclient/WebClientApplication.java @@ -15,11 +15,10 @@ public class WebClientApplication { } @Bean - public SecurityWebFilterChain functionalValidationsSpringSecurityFilterChain(ServerHttpSecurity http) { - http.authorizeExchange() - .anyExchange() - .permitAll(); - http.csrf().disable(); + public SecurityWebFilterChain filterChain(ServerHttpSecurity http) { + http.csrf().disable() + .authorizeExchange() + .anyExchange().permitAll(); return http.build(); } } diff --git a/spring-reactive/src/test/java/com/baeldung/reactive/webclient/WebControllerIntegrationTest.java b/spring-reactive/src/test/java/com/baeldung/reactive/webclient/WebControllerIntegrationTest.java index 3ab687bb41..22bd2829d3 100644 --- a/spring-reactive/src/test/java/com/baeldung/reactive/webclient/WebControllerIntegrationTest.java +++ b/spring-reactive/src/test/java/com/baeldung/reactive/webclient/WebControllerIntegrationTest.java @@ -1,22 +1,22 @@ package com.baeldung.reactive.webclient; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.reactive.server.WebTestClient; -@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS) -@RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = WebClientApplication.class) +import static org.springframework.test.annotation.DirtiesContext.ClassMode.BEFORE_CLASS; + +@DirtiesContext(classMode = BEFORE_CLASS) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = WebClientApplication.class) public class WebControllerIntegrationTest { @LocalServerPort - int randomServerPort; + private int randomServerPort; @Autowired private WebTestClient testClient; @@ -24,30 +24,26 @@ public class WebControllerIntegrationTest { @Autowired private WebController webController; - @Before - public void setup() { + @BeforeEach + void setup() { webController.setServerPort(randomServerPort); } @Test - public void whenEndpointWithBlockingClientIsCalled_thenThreeTweetsAreReceived() { + void whenEndpointWithBlockingClientIsCalled_thenThreeTweetsAreReceived() { testClient.get() .uri("/tweets-blocking") .exchange() - .expectStatus() - .isOk() - .expectBodyList(Tweet.class) - .hasSize(3); + .expectStatus().isOk() + .expectBodyList(Tweet.class).hasSize(3); } @Test - public void whenEndpointWithNonBlockingClientIsCalled_thenThreeTweetsAreReceived() { + void whenEndpointWithNonBlockingClientIsCalled_thenThreeTweetsAreReceived() { testClient.get() .uri("/tweets-non-blocking") .exchange() - .expectStatus() - .isOk() - .expectBodyList(Tweet.class) - .hasSize(3); + .expectStatus().isOk() + .expectBodyList(Tweet.class).hasSize(3); } } \ No newline at end of file diff --git a/spring-reactive/src/test/java/com/baeldung/reactive/webclient/WebTestClientIntegrationTest.java b/spring-reactive/src/test/java/com/baeldung/reactive/webclient/WebTestClientIntegrationTest.java index 90f4a0eca2..dc2a2a30b9 100644 --- a/spring-reactive/src/test/java/com/baeldung/reactive/webclient/WebTestClientIntegrationTest.java +++ b/spring-reactive/src/test/java/com/baeldung/reactive/webclient/WebTestClientIntegrationTest.java @@ -3,6 +3,7 @@ package com.baeldung.reactive.webclient; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.context.ApplicationContext; import org.springframework.security.test.context.support.WithMockUser; @@ -14,7 +15,7 @@ import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.server.WebHandler; import reactor.core.publisher.Mono; -@SpringBootTest(classes = WebClientApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@SpringBootTest(classes = WebClientApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT) public class WebTestClientIntegrationTest { @LocalServerPort @@ -26,73 +27,61 @@ public class WebTestClientIntegrationTest { @Autowired private WebClientController controller; - private final RouterFunction ROUTER_FUNCTION = RouterFunctions.route(RequestPredicates.GET("/resource"), request -> ServerResponse.ok() - .build()); - private final WebHandler WEB_HANDLER = exchange -> Mono.empty(); - @Test - public void testWebTestClientWithServerWebHandler() { - WebTestClient.bindToWebHandler(WEB_HANDLER) - .build(); + public void whenBindToWebHandler_thenRequestProcessed() { + WebHandler webHandler = exchange -> Mono.empty(); + + WebTestClient.bindToWebHandler(webHandler) + .build() + .get() + .exchange() + .expectBody().isEmpty(); } @Test - public void testWebTestClientWithRouterFunction() { - WebTestClient.bindToRouterFunction(ROUTER_FUNCTION) - .build() - .get() - .uri("/resource") - .exchange() - .expectStatus() - .isOk() - .expectBody() - .isEmpty(); + public void whenBindToRouter_thenRequestProcessed() { + RouterFunction routerFunction = RouterFunctions.route( + RequestPredicates.GET("/resource"), + request -> ServerResponse.ok().build() + ); + + WebTestClient.bindToRouterFunction(routerFunction) + .build() + .get().uri("/resource") + .exchange() + .expectStatus().isOk() + .expectBody().isEmpty(); } @Test @WithMockUser - public void testWebTestClientWithServerURL() { + public void whenBindToServer_thenRequestProcessed() { WebTestClient.bindToServer() - .baseUrl("http://localhost:" + port) - .build() - .get() - .uri("/resource") - .exchange() - .expectStatus() - .isOk() - .expectBody() - .jsonPath("field") - .isEqualTo("value"); - ; + .baseUrl("http://localhost:" + port).build() + .get().uri("/resource") + .exchange() + .expectStatus().isOk() + .expectBody().jsonPath("field").isEqualTo("value"); } @Test @WithMockUser - public void testWebTestClientWithApplicationContext() { + public void whenBindToApplicationContext_thenRequestProcessed() { WebTestClient.bindToApplicationContext(context) - .build() - .get() - .uri("/resource") - .exchange() - .expectStatus() - .isOk() - .expectBody() - .jsonPath("field") - .isEqualTo("value"); + .build() + .get().uri("/resource") + .exchange() + .expectStatus().isOk() + .expectBody().jsonPath("field").isEqualTo("value"); } @Test - public void testWebTestClientWithController() { + public void whenBindToController_thenRequestProcessed() { WebTestClient.bindToController(controller) - .build() - .get() - .uri("/resource") - .exchange() - .expectStatus() - .isOk() - .expectBody() - .jsonPath("field") - .isEqualTo("value"); + .build() + .get().uri("/resource") + .exchange() + .expectStatus().isOk() + .expectBody().jsonPath("field").isEqualTo("value"); } - }