[JAVA-8145] Update and cleanup code

This commit is contained in:
Haroon Khan 2022-04-30 12:27:13 +01:00
parent ab97eda9d9
commit 6df424bff4
4 changed files with 67 additions and 93 deletions

View File

@ -1,24 +1,14 @@
package com.baeldung.reactive.webclient; package com.baeldung.reactive.webclient;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Foo { public class Foo {
private String name; 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;
}
} }

View File

@ -15,11 +15,10 @@ public class WebClientApplication {
} }
@Bean @Bean
public SecurityWebFilterChain functionalValidationsSpringSecurityFilterChain(ServerHttpSecurity http) { public SecurityWebFilterChain filterChain(ServerHttpSecurity http) {
http.authorizeExchange() http.csrf().disable()
.anyExchange() .authorizeExchange()
.permitAll(); .anyExchange().permitAll();
http.csrf().disable();
return http.build(); return http.build();
} }
} }

View File

@ -1,22 +1,22 @@
package com.baeldung.reactive.webclient; package com.baeldung.reactive.webclient;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; 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.boot.web.server.LocalServerPort;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS) import static org.springframework.test.annotation.DirtiesContext.ClassMode.BEFORE_CLASS;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = WebClientApplication.class) @DirtiesContext(classMode = BEFORE_CLASS)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = WebClientApplication.class)
public class WebControllerIntegrationTest { public class WebControllerIntegrationTest {
@LocalServerPort @LocalServerPort
int randomServerPort; private int randomServerPort;
@Autowired @Autowired
private WebTestClient testClient; private WebTestClient testClient;
@ -24,30 +24,26 @@ public class WebControllerIntegrationTest {
@Autowired @Autowired
private WebController webController; private WebController webController;
@Before @BeforeEach
public void setup() { void setup() {
webController.setServerPort(randomServerPort); webController.setServerPort(randomServerPort);
} }
@Test @Test
public void whenEndpointWithBlockingClientIsCalled_thenThreeTweetsAreReceived() { void whenEndpointWithBlockingClientIsCalled_thenThreeTweetsAreReceived() {
testClient.get() testClient.get()
.uri("/tweets-blocking") .uri("/tweets-blocking")
.exchange() .exchange()
.expectStatus() .expectStatus().isOk()
.isOk() .expectBodyList(Tweet.class).hasSize(3);
.expectBodyList(Tweet.class)
.hasSize(3);
} }
@Test @Test
public void whenEndpointWithNonBlockingClientIsCalled_thenThreeTweetsAreReceived() { void whenEndpointWithNonBlockingClientIsCalled_thenThreeTweetsAreReceived() {
testClient.get() testClient.get()
.uri("/tweets-non-blocking") .uri("/tweets-non-blocking")
.exchange() .exchange()
.expectStatus() .expectStatus().isOk()
.isOk() .expectBodyList(Tweet.class).hasSize(3);
.expectBodyList(Tweet.class)
.hasSize(3);
} }
} }

View File

@ -3,6 +3,7 @@ package com.baeldung.reactive.webclient;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; 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.boot.web.server.LocalServerPort;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.security.test.context.support.WithMockUser; 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 org.springframework.web.server.WebHandler;
import reactor.core.publisher.Mono; 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 { public class WebTestClientIntegrationTest {
@LocalServerPort @LocalServerPort
@ -26,73 +27,61 @@ public class WebTestClientIntegrationTest {
@Autowired @Autowired
private WebClientController controller; 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 @Test
public void testWebTestClientWithServerWebHandler() { public void whenBindToWebHandler_thenRequestProcessed() {
WebTestClient.bindToWebHandler(WEB_HANDLER) WebHandler webHandler = exchange -> Mono.empty();
.build();
WebTestClient.bindToWebHandler(webHandler)
.build()
.get()
.exchange()
.expectBody().isEmpty();
} }
@Test @Test
public void testWebTestClientWithRouterFunction() { public void whenBindToRouter_thenRequestProcessed() {
WebTestClient.bindToRouterFunction(ROUTER_FUNCTION) RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(
RequestPredicates.GET("/resource"),
request -> ServerResponse.ok().build()
);
WebTestClient.bindToRouterFunction(routerFunction)
.build() .build()
.get() .get().uri("/resource")
.uri("/resource")
.exchange() .exchange()
.expectStatus() .expectStatus().isOk()
.isOk() .expectBody().isEmpty();
.expectBody()
.isEmpty();
} }
@Test @Test
@WithMockUser @WithMockUser
public void testWebTestClientWithServerURL() { public void whenBindToServer_thenRequestProcessed() {
WebTestClient.bindToServer() WebTestClient.bindToServer()
.baseUrl("http://localhost:" + port) .baseUrl("http://localhost:" + port).build()
.build() .get().uri("/resource")
.get()
.uri("/resource")
.exchange() .exchange()
.expectStatus() .expectStatus().isOk()
.isOk() .expectBody().jsonPath("field").isEqualTo("value");
.expectBody()
.jsonPath("field")
.isEqualTo("value");
;
} }
@Test @Test
@WithMockUser @WithMockUser
public void testWebTestClientWithApplicationContext() { public void whenBindToApplicationContext_thenRequestProcessed() {
WebTestClient.bindToApplicationContext(context) WebTestClient.bindToApplicationContext(context)
.build() .build()
.get() .get().uri("/resource")
.uri("/resource")
.exchange() .exchange()
.expectStatus() .expectStatus().isOk()
.isOk() .expectBody().jsonPath("field").isEqualTo("value");
.expectBody()
.jsonPath("field")
.isEqualTo("value");
} }
@Test @Test
public void testWebTestClientWithController() { public void whenBindToController_thenRequestProcessed() {
WebTestClient.bindToController(controller) WebTestClient.bindToController(controller)
.build() .build()
.get() .get().uri("/resource")
.uri("/resource")
.exchange() .exchange()
.expectStatus() .expectStatus().isOk()
.isOk() .expectBody().jsonPath("field").isEqualTo("value");
.expectBody()
.jsonPath("field")
.isEqualTo("value");
} }
} }