Browse Source

Introduce test for WebTestClient with RegEx-based JsonPath

pull/1739/head
Sam Brannen 7 years ago
parent
commit
00fea23e6b
  1. 20
      spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/JsonContentTests.java

20
spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/JsonContentTests.java

@ -20,11 +20,13 @@ import java.net.URI; @@ -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 { @@ -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 { @@ -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();

Loading…
Cancel
Save