Support target type in MockMvcResultMatchers.jsonPath()
This commit introduces an overloaded jsonPath() method to specify a target type to coerce into for MockMvcResultMatchers. - jsonPath(String, Matcher<T>, Class<T>) Closes gh-23141
This commit is contained in:
parent
89ebdc766c
commit
72adc3d37e
|
|
@ -204,6 +204,20 @@ public abstract class MockMvcResultMatchers {
|
|||
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>
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
public static <T> ResultMatcher jsonPath(String expression, Class<T> targetClass, Matcher<T> matcher) {
|
||||
return new JsonPathResultMatchers(expression).value(matcher, targetClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Access to response body assertions using an XPath expression to
|
||||
* inspect a specific subset of the body.
|
||||
|
|
|
|||
|
|
@ -24,8 +24,19 @@ 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,6 +67,20 @@ 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("Dilbert");
|
||||
return new Person(id, "Dilbert");
|
||||
}
|
||||
return new Person("Wally");
|
||||
return new Person(id, "Wally");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue