Skip to content

Support target type in MockMvcResultMatchers.jsonPath() #23141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,20 @@ 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 &mdash; 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,19 @@
*/
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ void getPerson42() throws Exception {
.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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

}