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 84e14aa7634..11b9567513b 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 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.
+ *
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 117c0f75939..96f547ce40c 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 @@ 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;
}
}
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 b12292b099d..e9664d0c689 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 @@ 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))
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 fc5e17b4cf2..a85d690c450 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");
}
}