parent
72adc3d37e
commit
ef6471fcbf
|
|
@ -187,35 +187,42 @@ public abstract class MockMvcResultMatchers {
|
|||
* {@link String#format(String, Object...)}.
|
||||
* @param expression the JSON path expression, optionally parameterized with arguments
|
||||
* @param args arguments to parameterize the JSON path expression with
|
||||
* @see #jsonPath(String, Matcher)
|
||||
* @see #jsonPath(String, Matcher, Class)
|
||||
*/
|
||||
public static JsonPathResultMatchers jsonPath(String expression, Object... args) {
|
||||
return new JsonPathResultMatchers(expression, args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Access to response body assertions using a
|
||||
* <a href="https://github.com/jayway/JsonPath">JsonPath</a> expression
|
||||
* to inspect a specific subset of the body and a Hamcrest matcher for
|
||||
* asserting the value found at the JSON path.
|
||||
* Evaluate the given <a href="https://github.com/jayway/JsonPath">JsonPath</a>
|
||||
* expression against the response body and assert the resulting value with
|
||||
* the given Hamcrest {@link Matcher}.
|
||||
* @param expression the JSON path expression
|
||||
* @param matcher a matcher for the value expected at the JSON path
|
||||
* @see #jsonPath(String, Object...)
|
||||
* @see #jsonPath(String, Matcher, Class)
|
||||
*/
|
||||
public static <T> ResultMatcher jsonPath(String expression, Matcher<T> matcher) {
|
||||
return new JsonPathResultMatchers(expression).value(matcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* An overloaded variant of {@link #jsonPath(String, Matcher)} (Matcher)} that also accepts
|
||||
* a target type for the resulting value that the matcher can work reliably against.
|
||||
* <p> This can be useful for matching numbers reliably — for example,
|
||||
* to coerce an integer into a double.</p>
|
||||
*
|
||||
* Evaluate the given <a href="https://github.com/jayway/JsonPath">JsonPath</a>
|
||||
* expression against the response body and assert the resulting value with
|
||||
* the given Hamcrest {@link Matcher}, coercing the resulting value into the
|
||||
* given target type before applying the matcher.
|
||||
* <p>This can be useful for matching numbers reliably — for example,
|
||||
* to coerce an integer into a double.
|
||||
* @param expression the JSON path expression
|
||||
* @param targetClass the target class to coerce the matching type into.
|
||||
* @param matcher a matcher for the value expected at the JSON path
|
||||
* @param targetType the target type to coerce the matching value into
|
||||
* @since 5.2
|
||||
* @see #jsonPath(String, Object...)
|
||||
* @see #jsonPath(String, Matcher)
|
||||
*/
|
||||
public static <T> ResultMatcher jsonPath(String expression, Class<T> targetClass, Matcher<T> matcher) {
|
||||
return new JsonPathResultMatchers(expression).value(matcher, targetClass);
|
||||
public static <T> ResultMatcher jsonPath(String expression, Matcher<T> matcher, Class<T> targetType) {
|
||||
return new JsonPathResultMatchers(expression).value(matcher, targetType);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -24,19 +24,8 @@ package org.springframework.test.context.junit.jupiter.comics;
|
|||
*/
|
||||
public class Person extends Character {
|
||||
|
||||
private final long id;
|
||||
|
||||
public Person(String name) {
|
||||
this(0, name);
|
||||
}
|
||||
|
||||
public Person(long id, String name) {
|
||||
super(name);
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -67,20 +67,6 @@ class MultipleWebRequestsSpringExtensionTests {
|
|||
.andExpect(jsonPath("$.name", is("Dilbert")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getPerson1() throws Exception {
|
||||
// Tests for #23121 (Target type in jsonPath method of MockMvcResultMatchers) coercing into Long
|
||||
this.mockMvc.perform(get("/person/1").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(jsonPath("$.id", Long.class, is(1L)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getPerson2() throws Exception {
|
||||
// Tests for #23121 (Target type in jsonPath method of MockMvcResultMatchers) coercing into String
|
||||
this.mockMvc.perform(get("/person/2").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(jsonPath("$.id", String.class, is("2")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getPerson99() throws Exception {
|
||||
this.mockMvc.perform(get("/person/99").accept(MediaType.APPLICATION_JSON))
|
||||
|
|
|
|||
|
|
@ -31,9 +31,9 @@ class PersonController {
|
|||
@GetMapping("/person/{id}")
|
||||
Person getPerson(@PathVariable long id) {
|
||||
if (id == 42) {
|
||||
return new Person(id, "Dilbert");
|
||||
return new Person("Dilbert");
|
||||
}
|
||||
return new Person(id, "Wally");
|
||||
return new Person("Wally");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,15 +16,16 @@
|
|||
|
||||
package org.springframework.test.web.servlet.samples.standalone;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.test.web.Person;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
|
|
@ -35,6 +36,7 @@ import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standal
|
|||
* Response written from {@code @ResponseBody} method.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class ResponseBodyTests {
|
||||
|
||||
|
|
@ -44,17 +46,49 @@ public class ResponseBodyTests {
|
|||
.perform(get("/person/Lee").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType("application/json"))
|
||||
.andExpect(jsonPath("$.name").value("Lee"));
|
||||
.andExpect(jsonPath("$.name").value("Lee"))
|
||||
.andExpect(jsonPath("$.age").value(42))
|
||||
.andExpect(jsonPath("$.age").value(42.0f))
|
||||
.andExpect(jsonPath("$.age").value(equalTo(42)))
|
||||
.andExpect(jsonPath("$.age").value(equalTo(42.0f), Float.class))
|
||||
.andExpect(jsonPath("$.age", equalTo(42)))
|
||||
.andExpect(jsonPath("$.age", equalTo(42.0f), Float.class));
|
||||
}
|
||||
|
||||
|
||||
@Controller
|
||||
private class PersonController {
|
||||
@RestController
|
||||
private static class PersonController {
|
||||
|
||||
@RequestMapping(value="/person/{name}")
|
||||
@ResponseBody
|
||||
@GetMapping("/person/{name}")
|
||||
public Person get(@PathVariable String name) {
|
||||
return new Person(name);
|
||||
Person person = new Person(name);
|
||||
person.setAge(42);
|
||||
return person;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class Person {
|
||||
|
||||
@NotNull
|
||||
private final String name;
|
||||
|
||||
private int age;
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return this.age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue