parent
920be8e1b2
commit
eb84c84373
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
* Copyright 2002-2022 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.
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
* Copyright 2002-2022 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.
|
||||
|
|
@ -973,11 +973,14 @@ public interface WebTestClient {
|
|||
|
||||
/**
|
||||
* Parse the expected and actual response content as JSON and perform a
|
||||
* "lenient" comparison verifying the same attribute-value pairs.
|
||||
* <p>Use of this option requires the
|
||||
* comparison verifying that they contain the same attribute-value pairs
|
||||
* regardless of formatting with <em>lenient</em> checking (extensible
|
||||
* and non-strict array ordering).
|
||||
* <p>Use of this method requires the
|
||||
* <a href="https://jsonassert.skyscreamer.org/">JSONassert</a> library
|
||||
* on to be on the classpath.
|
||||
* @param expectedJson the expected JSON content.
|
||||
* to be on the classpath.
|
||||
* @param expectedJson the expected JSON content
|
||||
* @see #json(String, boolean)
|
||||
*/
|
||||
default BodyContentSpec json(String expectedJson) {
|
||||
return json(expectedJson, false);
|
||||
|
|
@ -985,16 +988,20 @@ public interface WebTestClient {
|
|||
|
||||
/**
|
||||
* Parse the expected and actual response content as JSON and perform a
|
||||
* comparison in two modes, depending on {@code strict} parameter value, verifying the same attribute-value pairs.
|
||||
* comparison verifying that they contain the same attribute-value pairs
|
||||
* regardless of formatting.
|
||||
* <p>Can compare in two modes, depending on the {@code strict} parameter value:
|
||||
* <ul>
|
||||
* <li>{@code true}: strict checking.
|
||||
* <li>{@code false}: lenient checking.
|
||||
* <li>{@code true}: strict checking. Not extensible and strict array ordering.</li>
|
||||
* <li>{@code false}: lenient checking. Extensible and non-strict array ordering.</li>
|
||||
* </ul>
|
||||
* <p>Use of this option requires the
|
||||
* <p>Use of this method requires the
|
||||
* <a href="https://jsonassert.skyscreamer.org/">JSONassert</a> library
|
||||
* on to be on the classpath.
|
||||
* @param expectedJson the expected JSON content.
|
||||
* @param strict enables strict checking
|
||||
* to be on the classpath.
|
||||
* @param expectedJson the expected JSON content
|
||||
* @param strict enables strict checking if {@code true}
|
||||
* @since 5.3.16
|
||||
* @see #json(String)
|
||||
*/
|
||||
BodyContentSpec json(String expectedJson, boolean strict);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2022 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.
|
||||
|
|
@ -34,7 +34,6 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
|
||||
|
||||
/**
|
||||
* Samples of tests using {@link WebTestClient} with serialized JSON content.
|
||||
*
|
||||
|
|
@ -48,33 +47,43 @@ public class JsonContentTests {
|
|||
|
||||
|
||||
@Test
|
||||
public void jsonContent() {
|
||||
this.client.get().uri("/persons/extended")
|
||||
public void jsonContentWithDefaultLenientMode() {
|
||||
this.client.get().uri("/persons")
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody().json("[{\"name\":\"Jane\"},{\"name\":\"Jason\"},{\"name\":\"John\"}]");
|
||||
.expectBody().json(
|
||||
"[{\"firstName\":\"Jane\"}," +
|
||||
"{\"firstName\":\"Jason\"}," +
|
||||
"{\"firstName\":\"John\"}]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonContentStrictFail() {
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> this.client.get().uri("/persons/extended")
|
||||
public void jsonContentWithStrictMode() {
|
||||
this.client.get().uri("/persons")
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody().json("[{\"name\":\"Jane\"},{\"name\":\"Jason\"},{\"name\":\"John\"}]", true)
|
||||
.expectBody().json(
|
||||
"[{\"firstName\":\"Jane\",\"lastName\":\"Williams\"}," +
|
||||
"{\"firstName\":\"Jason\",\"lastName\":\"Johnson\"}," +
|
||||
"{\"firstName\":\"John\",\"lastName\":\"Smith\"}]",
|
||||
true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonContentWithStrictModeAndMissingAttributes() {
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> this.client.get().uri("/persons")
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.exchange()
|
||||
.expectBody().json(
|
||||
"[{\"firstName\":\"Jane\"}," +
|
||||
"{\"firstName\":\"Jason\"}," +
|
||||
"{\"firstName\":\"John\"}]",
|
||||
true)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonContentStrict() {
|
||||
this.client.get().uri("/persons/extended")
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody().json("[{\"name\":\"Jane\",\"surname\":\"Williams\"},{\"name\":\"Jason\",\"surname\":\"Johnson\"},{\"name\":\"John\",\"surname\":\"Smith\"}]", true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonPathIsEqualTo() {
|
||||
this.client.get().uri("/persons")
|
||||
|
|
@ -82,26 +91,26 @@ public class JsonContentTests {
|
|||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody()
|
||||
.jsonPath("$[0].name").isEqualTo("Jane")
|
||||
.jsonPath("$[1].name").isEqualTo("Jason")
|
||||
.jsonPath("$[2].name").isEqualTo("John");
|
||||
.jsonPath("$[0].firstName").isEqualTo("Jane")
|
||||
.jsonPath("$[1].firstName").isEqualTo("Jason")
|
||||
.jsonPath("$[2].firstName").isEqualTo("John");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonPathMatches() {
|
||||
this.client.get().uri("/persons/John")
|
||||
this.client.get().uri("/persons/John/Smith")
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody()
|
||||
.jsonPath("$.name").value(containsString("oh"));
|
||||
.jsonPath("$.firstName").value(containsString("oh"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postJsonContent() {
|
||||
this.client.post().uri("/persons")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.bodyValue("{\"name\":\"John\"}")
|
||||
.bodyValue("{\"firstName\":\"John\",\"lastName\":\"Smith\"}")
|
||||
.exchange()
|
||||
.expectStatus().isCreated()
|
||||
.expectBody().isEmpty();
|
||||
|
|
@ -114,40 +123,38 @@ public class JsonContentTests {
|
|||
|
||||
@GetMapping
|
||||
Flux<Person> getPersons() {
|
||||
return Flux.just(new Person("Jane"), new Person("Jason"), new Person("John"));
|
||||
return Flux.just(new Person("Jane", "Williams"), new Person("Jason", "Johnson"), new Person("John", "Smith"));
|
||||
}
|
||||
|
||||
@GetMapping("/extended")
|
||||
Flux<ExtendedPerson> getExtendedPersons() {
|
||||
return Flux.just(new ExtendedPerson("Jane", "Williams"), new ExtendedPerson("Jason", "Johnson"), new ExtendedPerson("John", "Smith"));
|
||||
}
|
||||
|
||||
@GetMapping("/{name}")
|
||||
Person getPerson(@PathVariable String name) {
|
||||
return new Person(name);
|
||||
@GetMapping("/{firstName}/{lastName}")
|
||||
Person getPerson(@PathVariable String firstName, @PathVariable String lastName) {
|
||||
return new Person(firstName, lastName);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
ResponseEntity<String> savePerson(@RequestBody Person person) {
|
||||
return ResponseEntity.created(URI.create("/persons/" + person.getName())).build();
|
||||
return ResponseEntity.created(URI.create(String.format("/persons/%s/%s", person.getFirstName(), person.getLastName()))).build();
|
||||
}
|
||||
}
|
||||
|
||||
static class ExtendedPerson {
|
||||
private String name;
|
||||
private String surname;
|
||||
static class Person {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public ExtendedPerson(String name, String surname) {
|
||||
this.name = name;
|
||||
this.surname = surname;
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
public Person(String firstName, String lastName) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getSurname() {
|
||||
return surname;
|
||||
public String getFirstName() {
|
||||
return this.firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return this.lastName;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue