diff --git a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/JsonContentTests.java b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/JsonContentTests.java index 2235625627..21ccd29185 100644 --- a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/JsonContentTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/JsonContentTests.java @@ -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 getPerson(@PathVariable String name) { + return Mono.just(new Person(name)); + } + @PostMapping ResponseEntity savePerson(@RequestBody Person person) { return ResponseEntity.created(URI.create("/persons/" + person.getName())).build();