WebTestClient assert response body with Consumer<B>

Issue: SPR-15421
This commit is contained in:
Rossen Stoyanchev
2017-04-13 17:14:48 -04:00
parent 0e84f246cb
commit 1e8c7e55de
6 changed files with 134 additions and 20 deletions

View File

@@ -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 {

View File

@@ -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"))

View File

@@ -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"))