Refactor WebTestClient assertions take 3

WebTestClient now defines all the steps from setup to performing
an exchange and applying expectations.

The order of expectations now ensures the response status and headers
are verified first since that's available before the body is consumed
and also because it determines how the body is to be decoded, i.e.
error vs success scenarios.

There is now a built-in option for verifying the response as a Map
along with Map-specific assertions.

There are similar options for verifying the response as a List as well
as whether to "collect" the list or "take" the first N elements from
the response stream.
This commit is contained in:
Rossen Stoyanchev
2017-02-17 22:58:32 -05:00
parent f1653cc21c
commit 9829a62044
16 changed files with 588 additions and 410 deletions

View File

@@ -33,6 +33,7 @@ import org.springframework.web.reactive.function.client.ClientRequest;
import org.springframework.web.reactive.function.client.ExchangeFunction;
import org.springframework.web.reactive.function.client.ExchangeFunctions;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
@@ -60,7 +61,8 @@ public class WiretapConnectorTests {
WiretapConnector.Info info = infoRef.get();
assertNotNull(info);
assertSame(request, info.getRequest());
assertEquals(HttpMethod.GET, info.getMethod());
assertEquals("/test", info.getUrl().toString());
assertSame(response, info.getResponse());
}

View File

@@ -44,18 +44,16 @@ public class ErrorTests {
public void notFound() throws Exception {
this.client.get().uri("/invalid")
.exchange()
.expectNoBody()
.assertThat()
.status().isNotFound();
.expectStatus().isNotFound()
.expectBody().isEmpty();
}
@Test
public void serverException() throws Exception {
this.client.get().uri("/server-error")
.exchange()
.expectNoBody()
.assertThat()
.status().isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
.expectStatus().isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR)
.expectBody().isEmpty();
}

View File

@@ -49,20 +49,16 @@ public class HeaderTests {
public void requestResponseHeaderPair() throws Exception {
this.client.get().uri("/request-response-pair").header("h1", "in")
.exchange()
.expectNoBody()
.assertThat()
.status().isOk()
.header().valueEquals("h1", "in-out");
.expectStatus().isOk()
.expectHeader().valueEquals("h1", "in-out");
}
@Test
public void headerMultivalue() throws Exception {
this.client.get().uri("/multivalue")
.exchange()
.expectNoBody()
.assertThat()
.status().isOk()
.header().valueEquals("h1", "v1", "v2", "v3");
.expectStatus().isOk()
.expectHeader().valueEquals("h1", "v1", "v2", "v3");
}

View File

@@ -18,6 +18,7 @@ package org.springframework.test.web.reactive.server.samples;
import java.net.URI;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonCreator;
@@ -28,7 +29,6 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import org.springframework.core.ResolvableType;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.web.reactive.server.ExchangeResult;
@@ -65,22 +65,22 @@ public class ResponseEntityTests {
public void entity() throws Exception {
this.client.get().uri("/persons/John")
.exchange()
.decodeEntity(Person.class)
.assertThat()
.status().isOk()
.header().contentTypeEquals(MediaType.APPLICATION_JSON_UTF8)
.bodyEquals(new Person("John"));
.expectStatus().isOk()
.expectHeader().contentTypeEquals(MediaType.APPLICATION_JSON_UTF8)
.expectBody(Person.class).value().isEqualTo(new Person("John"));
}
@Test
public void entityList() throws Exception {
List<Person> expected = Arrays.asList(
new Person("Jane"), new Person("Jason"), new Person("John"));
this.client.get().uri("/persons")
.exchange()
.decodeAndCollect(Person.class)
.assertThat()
.status().isOk()
.header().contentTypeEquals(MediaType.APPLICATION_JSON_UTF8)
.bodyEquals(Arrays.asList(new Person("Jane"), new Person("Jason"), new Person("John")));
.expectStatus().isOk()
.expectHeader().contentTypeEquals(MediaType.APPLICATION_JSON_UTF8)
.expectBody(Person.class).list().isEqualTo(expected);
}
@Test
@@ -93,10 +93,9 @@ public class ResponseEntityTests {
this.client.get().uri("/persons?map=true")
.exchange()
.decodeEntity(ResolvableType.forClassWithGenerics(Map.class, String.class, Person.class))
.assertThat()
.status().isOk()
.bodyEquals(map);
.expectStatus().isOk()
.expectBody()
.map(String.class, Person.class).isEqualTo(map);
}
@Test
@@ -106,11 +105,10 @@ public class ResponseEntityTests {
.uri("/persons")
.accept(TEXT_EVENT_STREAM)
.exchange()
.decodeFlux(Person.class);
result.assertThat()
.status().isOk()
.header().contentTypeEquals(TEXT_EVENT_STREAM);
.expectStatus().isOk()
.expectHeader().contentTypeEquals(TEXT_EVENT_STREAM)
.expectBody(Person.class)
.returnResult();
StepVerifier.create(result.getResponseBody())
.expectNext(new Person("N0"), new Person("N1"), new Person("N2"))
@@ -124,10 +122,9 @@ public class ResponseEntityTests {
public void postEntity() throws Exception {
this.client.post().uri("/persons")
.exchange(Mono.just(new Person("John")), Person.class)
.expectNoBody()
.assertThat()
.status().isCreated()
.header().valueEquals("location", "/persons/John");
.expectStatus().isCreated()
.expectHeader().valueEquals("location", "/persons/John")
.expectBody().isEmpty();
}

View File

@@ -51,10 +51,8 @@ public class ApplicationContextTests {
public void test() throws Exception {
this.client.get().uri("/test")
.exchange()
.decodeEntity(String.class)
.assertThat()
.status().isOk()
.bodyEquals("It works!");
.expectStatus().isOk()
.expectBody(String.class).value().isEqualTo("It works!");
}

View File

@@ -43,10 +43,8 @@ public class ControllerTests {
public void test() throws Exception {
this.client.get().uri("/test")
.exchange()
.decodeEntity(String.class)
.assertThat()
.status().isOk()
.bodyEquals("It works!");
.expectStatus().isOk()
.expectBody(String.class).value().isEqualTo("It works!");
}

View File

@@ -68,10 +68,8 @@ public class HttpServerTests {
public void test() throws Exception {
this.client.get().uri("/test")
.exchange()
.decodeEntity(String.class)
.assertThat()
.status().isOk()
.bodyEquals("It works!");
.expectStatus().isOk()
.expectBody(String.class).value().isEqualTo("It works!");
}
}

View File

@@ -49,10 +49,8 @@ public class RouterFunctionTests {
public void test() throws Exception {
this.testClient.get().uri("/test")
.exchange()
.decodeEntity(String.class)
.assertThat()
.status().isOk()
.bodyEquals("It works!");
.expectStatus().isOk()
.expectBody(String.class).value().isEqualTo("It works!");
}
}