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 11b9567513b..3230e4ca4d4 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 @@ -187,35 +187,42 @@ public abstract class MockMvcResultMatchers { * {@link String#format(String, Object...)}. * @param expression the JSON path expression, optionally parameterized with arguments * @param args arguments to parameterize the JSON path expression with + * @see #jsonPath(String, Matcher) + * @see #jsonPath(String, Matcher, Class) */ public static JsonPathResultMatchers jsonPath(String expression, Object... args) { return new JsonPathResultMatchers(expression, args); } /** - * Access to response body assertions using a - * JsonPath expression - * to inspect a specific subset of the body and a Hamcrest matcher for - * asserting the value found at the JSON path. + * Evaluate the given JsonPath + * expression against the response body and assert the resulting value with + * the given Hamcrest {@link Matcher}. * @param expression the JSON path expression * @param matcher a matcher for the value expected at the JSON path + * @see #jsonPath(String, Object...) + * @see #jsonPath(String, Matcher, Class) */ 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.

- * + * Evaluate the given JsonPath + * expression against the response body and assert the resulting value with + * the given Hamcrest {@link Matcher}, coercing the resulting value into the + * given target type before applying the matcher. + *

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 + * @param targetType the target type to coerce the matching value into + * @since 5.2 + * @see #jsonPath(String, Object...) + * @see #jsonPath(String, Matcher) */ - public static ResultMatcher jsonPath(String expression, Class targetClass, Matcher matcher) { - return new JsonPathResultMatchers(expression).value(matcher, targetClass); + public static ResultMatcher jsonPath(String expression, Matcher matcher, Class targetType) { + return new JsonPathResultMatchers(expression).value(matcher, targetType); } /** 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 96f547ce40c..117c0f75939 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,19 +24,8 @@ 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 e9664d0c689..b12292b099d 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,20 +67,6 @@ 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 a85d690c450..fc5e17b4cf2 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(id, "Dilbert"); + return new Person("Dilbert"); } - return new Person(id, "Wally"); + return new Person("Wally"); } } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ResponseBodyTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ResponseBodyTests.java index 9122b1a8c49..9846ea79ca9 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ResponseBodyTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ResponseBodyTests.java @@ -16,15 +16,16 @@ package org.springframework.test.web.servlet.samples.standalone; +import javax.validation.constraints.NotNull; + import org.junit.Test; import org.springframework.http.MediaType; -import org.springframework.stereotype.Controller; -import org.springframework.test.web.Person; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; +import static org.hamcrest.Matchers.equalTo; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; @@ -35,6 +36,7 @@ import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standal * Response written from {@code @ResponseBody} method. * * @author Rossen Stoyanchev + * @author Sam Brannen */ public class ResponseBodyTests { @@ -44,17 +46,49 @@ public class ResponseBodyTests { .perform(get("/person/Lee").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType("application/json")) - .andExpect(jsonPath("$.name").value("Lee")); + .andExpect(jsonPath("$.name").value("Lee")) + .andExpect(jsonPath("$.age").value(42)) + .andExpect(jsonPath("$.age").value(42.0f)) + .andExpect(jsonPath("$.age").value(equalTo(42))) + .andExpect(jsonPath("$.age").value(equalTo(42.0f), Float.class)) + .andExpect(jsonPath("$.age", equalTo(42))) + .andExpect(jsonPath("$.age", equalTo(42.0f), Float.class)); } - @Controller - private class PersonController { + @RestController + private static class PersonController { - @RequestMapping(value="/person/{name}") - @ResponseBody + @GetMapping("/person/{name}") public Person get(@PathVariable String name) { - return new Person(name); + Person person = new Person(name); + person.setAge(42); + return person; + } + } + + @SuppressWarnings("unused") + private static class Person { + + @NotNull + private final String name; + + private int age; + + public Person(String name) { + this.name = name; + } + + public String getName() { + return this.name; + } + + public int getAge() { + return this.age; + } + + public void setAge(int age) { + this.age = age; } }