[JAVA-8145] Update and cleanup code
This commit is contained in:
parent
ab97eda9d9
commit
6df424bff4
spring-reactive/src
main/java/com/baeldung/reactive/webclient
test/java/com/baeldung/reactive/webclient
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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<ServerResponse> 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");
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user