diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultMatchers.java index 84e14aa7634c..11b9567513b7 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultMatchers.java @@ -204,6 +204,20 @@ public static ResultMatcher jsonPath(String expression, Matcher 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. + *

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 + */ + public static ResultMatcher jsonPath(String expression, Class targetClass, Matcher 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. diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/comics/Person.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/comics/Person.java index 117c0f759394..96f547ce40c1 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/comics/Person.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/comics/Person.java @@ -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; } } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/web/MultipleWebRequestsSpringExtensionTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/web/MultipleWebRequestsSpringExtensionTests.java index b12292b099d2..e9664d0c6893 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/web/MultipleWebRequestsSpringExtensionTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/web/MultipleWebRequestsSpringExtensionTests.java @@ -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)) diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/web/PersonController.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/web/PersonController.java index fc5e17b4cf21..a85d690c450e 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/web/PersonController.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/web/PersonController.java @@ -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"); } }