Hamcrest methods in WebTestClient

Issue: SPR-16729
This commit is contained in:
Rossen Stoyanchev
2018-07-20 17:37:12 -04:00
parent 0c62d6b5da
commit 20de5003ff
11 changed files with 210 additions and 55 deletions

View File

@@ -30,6 +30,7 @@ import org.springframework.http.MediaType;
import org.springframework.mock.http.client.reactive.MockClientHttpRequest;
import org.springframework.mock.http.client.reactive.MockClientHttpResponse;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
@@ -125,6 +126,15 @@ public class HeaderAssertionTests {
}
}
@Test
public void valueMatcher() {
HttpHeaders headers = new HttpHeaders();
headers.add("foo", "bar");
HeaderAssertions assertions = headerAssertions(headers);
assertions.value("foo", containsString("a"));
}
@Test
public void exists() {
HttpHeaders headers = new HttpHeaders();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@ import org.springframework.http.HttpStatus;
import org.springframework.mock.http.client.reactive.MockClientHttpRequest;
import org.springframework.mock.http.client.reactive.MockClientHttpResponse;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
@@ -158,6 +159,23 @@ public class StatusAssertionTests {
}
}
@Test
public void matches() {
StatusAssertions assertions = statusAssertions(HttpStatus.CONFLICT);
// Success
assertions.value(equalTo(409));
assertions.value(greaterThan(400));
try {
assertions.value(equalTo(200));
fail("Wrong status expected");
}
catch (AssertionError error) {
// Expected
}
}
private StatusAssertions statusAssertions(HttpStatus status) {
MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("/"));

View File

@@ -31,6 +31,8 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static org.hamcrest.Matchers.*;
/**
* Samples of tests using {@link WebTestClient} with serialized JSON content.
*
@@ -64,16 +66,14 @@ public class JsonContentTests {
.jsonPath("$[2].name").isEqualTo("John");
}
@Test // https://stackoverflow.com/questions/49149376/webtestclient-check-that-jsonpath-contains-sub-string
public void jsonPathContainsSubstringViaRegex() {
@Test
public void jsonPathMatches() {
this.client.get().uri("/persons/John")
.accept(MediaType.APPLICATION_JSON_UTF8)
.exchange()
.expectStatus().isOk()
.expectBody()
// The following determines if at least one person is returned with a
// name containing "oh", and "John" matches that.
.jsonPath("$[?(@.name =~ /.*oh.*/)].name").hasJsonPath();
.jsonPath("$.name").value(containsString("oh"));
}
@Test

View File

@@ -38,11 +38,10 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
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.http.MediaType.TEXT_EVENT_STREAM;
import static java.time.Duration.*;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.http.MediaType.*;
/**
* Annotated controllers accepting and returning typed Objects.
@@ -67,6 +66,15 @@ public class ResponseEntityTests {
.expectBody(Person.class).isEqualTo(new Person("John"));
}
@Test
public void entityMatcher() {
this.client.get().uri("/John")
.exchange()
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
.expectBody(Person.class).value(Person::getName, startsWith("Joh"));
}
@Test
public void entityWithConsumer() {
this.client.get().uri("/John")

View File

@@ -38,6 +38,8 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static org.hamcrest.Matchers.*;
/**
* Samples of tests using {@link WebTestClient} with XML content.
*
@@ -83,6 +85,16 @@ public class XmlContentTests {
.xpath("/persons/person[3]/name").isEqualTo("John");
}
@Test
public void xpathMatches() {
this.client.get().uri("/persons")
.accept(MediaType.APPLICATION_XML)
.exchange()
.expectStatus().isOk()
.expectBody()
.xpath("//person/name").string(startsWith("J"));
}
@Test
public void xpathContainsSubstringViaRegex() {
this.client.get().uri("/persons/John")