WebTestClient assert response body with Consumer<B>
Issue: SPR-15421
This commit is contained in:
@@ -42,10 +42,12 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import static java.time.Duration.ofMillis;
|
||||
import static org.hamcrest.CoreMatchers.endsWith;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.springframework.core.ResolvableType.forClassWithGenerics;
|
||||
import static org.springframework.http.MediaType.TEXT_EVENT_STREAM;
|
||||
|
||||
|
||||
/**
|
||||
* Annotated controllers accepting and returning typed Objects.
|
||||
*
|
||||
@@ -66,6 +68,15 @@ public class ResponseEntityTests {
|
||||
.expectBody(Person.class).isEqualTo(new Person("John"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void entityWithConsumer() throws Exception {
|
||||
this.client.get().uri("/persons/John")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
|
||||
.expectBody(Person.class).consumeWith(p -> assertEquals(new Person("John"), p));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void entityList() throws Exception {
|
||||
|
||||
|
||||
@@ -34,6 +34,8 @@ import org.springframework.web.reactive.config.EnableWebFlux;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebFilter;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Binding to server infrastructure declared in a Spring ApplicationContext.
|
||||
*
|
||||
@@ -58,14 +60,23 @@ public class ApplicationContextTests {
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void basic() throws Exception {
|
||||
public void bodyContent() throws Exception {
|
||||
this.client.get().uri("/principal")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody(String.class).isEqualTo("Hello Mr. Pablo!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bodyContentWithConsumer() throws Exception {
|
||||
this.client.get().uri("/principal")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody().consumeAsStringWith(body -> assertEquals("Hello Mr. Pablo!", body));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void perRequestExchangeMutator() throws Exception {
|
||||
this.client.exchangeMutator(principal("Giovanni"))
|
||||
|
||||
@@ -29,6 +29,8 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebFilter;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Bind to annotated controllers.
|
||||
*
|
||||
@@ -44,13 +46,21 @@ public class ControllerTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void basic() throws Exception {
|
||||
public void bodyContent() throws Exception {
|
||||
this.client.get().uri("/principal")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody(String.class).isEqualTo("Hello Mr. Pablo!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bodyContentWithConsumer() throws Exception {
|
||||
this.client.get().uri("/principal")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody().consumeAsStringWith(body -> assertEquals("Hello Mr. Pablo!", body));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void perRequestExchangeMutator() throws Exception {
|
||||
this.client.exchangeMutator(principal("Giovanni"))
|
||||
|
||||
Reference in New Issue
Block a user