Introduce test for WebTestClient with RegEx-based JsonPath

This commit is contained in:
Sam Brannen 2018-03-09 14:51:37 +01:00
parent 2a3f90dc7b
commit 00fea23e6b
1 changed files with 20 additions and 0 deletions

View File

@ -20,11 +20,13 @@ import java.net.URI;
import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@ -61,6 +63,19 @@ public class JsonContentTests {
.jsonPath("$[2].name").isEqualTo("John");
}
@Test
// See https://stackoverflow.com/questions/49149376/webtestclient-check-that-jsonpath-contains-sub-string
public void jsonPathContainsSubstringViaRegularExpression() throws Exception {
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();
}
@Test
public void postJsonContent() throws Exception {
this.client.post().uri("/persons")
@ -80,6 +95,11 @@ public class JsonContentTests {
return Flux.just(new Person("Jane"), new Person("Jason"), new Person("John"));
}
@GetMapping("/persons/{name}")
Mono<Person> getPerson(@PathVariable String name) {
return Mono.just(new Person(name));
}
@PostMapping
ResponseEntity<String> savePerson(@RequestBody Person person) {
return ResponseEntity.created(URI.create("/persons/" + person.getName())).build();