fixed and improved WebClient integration test

This commit is contained in:
Gerardo Roza 2021-01-22 12:33:54 -03:00
parent 814369e38c
commit 1b78d4be31
3 changed files with 65 additions and 9 deletions

View File

@ -0,0 +1,13 @@
package com.baeldung.web.reactive.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WebClientApplication{
public static void main(String[] args) {
SpringApplication.run(WebClientApplication.class, args);
}
}

View File

@ -4,6 +4,8 @@ import java.net.URI;
import java.nio.charset.Charset;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.reactivestreams.Publisher;
@ -35,7 +37,10 @@ public class WebClientController {
@ResponseStatus(HttpStatus.OK)
@GetMapping("/resource")
public void getResource() {
public Map<String, String> getResource() {
Map<String, String> response = new HashMap<>();
response.put("field", "value");
return response;
}
public void demonstrateWebClient() {

View File

@ -1,11 +1,11 @@
package com.baeldung.web.client;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.context.ApplicationContext;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
@ -13,18 +13,23 @@ import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.WebHandler;
import com.baeldung.reactive.Spring5ReactiveApplication;
import com.baeldung.web.reactive.client.WebClientApplication;
import com.baeldung.web.reactive.client.WebClientController;
import reactor.core.publisher.Mono;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Spring5ReactiveApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@WithMockUser
@SpringBootTest(classes = WebClientApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class WebTestClientIntegrationTest {
@LocalServerPort
private int port;
@Autowired
private ApplicationContext context;
@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();
@ -49,6 +54,7 @@ public class WebTestClientIntegrationTest {
}
@Test
@WithMockUser
public void testWebTestClientWithServerURL() {
WebTestClient.bindToServer()
.baseUrl("http://localhost:" + port)
@ -58,7 +64,39 @@ public class WebTestClientIntegrationTest {
.exchange()
.expectStatus()
.isOk()
.expectBody();
.expectBody()
.jsonPath("field")
.isEqualTo("value");
;
}
@Test
@WithMockUser
public void testWebTestClientWithApplicationContext() {
WebTestClient.bindToApplicationContext(context)
.build()
.get()
.uri("/resource")
.exchange()
.expectStatus()
.isOk()
.expectBody()
.jsonPath("field")
.isEqualTo("value");
}
@Test
public void testWebTestClientWithController() {
WebTestClient.bindToController(controller)
.build()
.get()
.uri("/resource")
.exchange()
.expectStatus()
.isOk()
.expectBody()
.jsonPath("field")
.isEqualTo("value");
}
}