Skip to content

Commit 72adc3d

Browse files
fodil-asbrannen
authored andcommitted
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
1 parent 89ebdc7 commit 72adc3d

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultMatchers.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,20 @@ public static <T> ResultMatcher jsonPath(String expression, Matcher<T> matcher)
204204
return new JsonPathResultMatchers(expression).value(matcher);
205205
}
206206

207+
/**
208+
* An overloaded variant of {@link #jsonPath(String, Matcher)} (Matcher)} that also accepts
209+
* a target type for the resulting value that the matcher can work reliably against.
210+
* <p> This can be useful for matching numbers reliably &mdash; for example,
211+
* to coerce an integer into a double.</p>
212+
*
213+
* @param expression the JSON path expression
214+
* @param targetClass the target class to coerce the matching type into.
215+
* @param matcher a matcher for the value expected at the JSON path
216+
*/
217+
public static <T> ResultMatcher jsonPath(String expression, Class<T> targetClass, Matcher<T> matcher) {
218+
return new JsonPathResultMatchers(expression).value(matcher, targetClass);
219+
}
220+
207221
/**
208222
* Access to response body assertions using an XPath expression to
209223
* inspect a specific subset of the body.

spring-test/src/test/java/org/springframework/test/context/junit/jupiter/comics/Person.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,19 @@
2424
*/
2525
public class Person extends Character {
2626

27+
private final long id;
28+
2729
public Person(String name) {
30+
this(0, name);
31+
}
32+
33+
public Person(long id, String name) {
2834
super(name);
35+
this.id = id;
36+
}
37+
38+
public long getId() {
39+
return id;
2940
}
3041

3142
}

spring-test/src/test/java/org/springframework/test/context/junit/jupiter/web/MultipleWebRequestsSpringExtensionTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ void getPerson42() throws Exception {
6767
.andExpect(jsonPath("$.name", is("Dilbert")));
6868
}
6969

70+
@Test
71+
void getPerson1() throws Exception {
72+
// Tests for #23121 (Target type in jsonPath method of MockMvcResultMatchers) coercing into Long
73+
this.mockMvc.perform(get("/person/1").accept(MediaType.APPLICATION_JSON))
74+
.andExpect(jsonPath("$.id", Long.class, is(1L)));
75+
}
76+
77+
@Test
78+
void getPerson2() throws Exception {
79+
// Tests for #23121 (Target type in jsonPath method of MockMvcResultMatchers) coercing into String
80+
this.mockMvc.perform(get("/person/2").accept(MediaType.APPLICATION_JSON))
81+
.andExpect(jsonPath("$.id", String.class, is("2")));
82+
}
83+
7084
@Test
7185
void getPerson99() throws Exception {
7286
this.mockMvc.perform(get("/person/99").accept(MediaType.APPLICATION_JSON))

spring-test/src/test/java/org/springframework/test/context/junit/jupiter/web/PersonController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ class PersonController {
3131
@GetMapping("/person/{id}")
3232
Person getPerson(@PathVariable long id) {
3333
if (id == 42) {
34-
return new Person("Dilbert");
34+
return new Person(id, "Dilbert");
3535
}
36-
return new Person("Wally");
36+
return new Person(id, "Wally");
3737
}
3838

3939
}

0 commit comments

Comments
 (0)