Skip to content

Commit ef6471f

Browse files
committed
Polish contribution
See gh-23141
1 parent 72adc3d commit ef6471f

File tree

5 files changed

+65
-49
lines changed

5 files changed

+65
-49
lines changed

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -187,35 +187,42 @@ public static ContentResultMatchers content() {
187187
* {@link String#format(String, Object...)}.
188188
* @param expression the JSON path expression, optionally parameterized with arguments
189189
* @param args arguments to parameterize the JSON path expression with
190+
* @see #jsonPath(String, Matcher)
191+
* @see #jsonPath(String, Matcher, Class)
190192
*/
191193
public static JsonPathResultMatchers jsonPath(String expression, Object... args) {
192194
return new JsonPathResultMatchers(expression, args);
193195
}
194196

195197
/**
196-
* Access to response body assertions using a
197-
* <a href="https://github.com/jayway/JsonPath">JsonPath</a> expression
198-
* to inspect a specific subset of the body and a Hamcrest matcher for
199-
* asserting the value found at the JSON path.
198+
* Evaluate the given <a href="https://github.com/jayway/JsonPath">JsonPath</a>
199+
* expression against the response body and assert the resulting value with
200+
* the given Hamcrest {@link Matcher}.
200201
* @param expression the JSON path expression
201202
* @param matcher a matcher for the value expected at the JSON path
203+
* @see #jsonPath(String, Object...)
204+
* @see #jsonPath(String, Matcher, Class)
202205
*/
203206
public static <T> ResultMatcher jsonPath(String expression, Matcher<T> matcher) {
204207
return new JsonPathResultMatchers(expression).value(matcher);
205208
}
206209

207210
/**
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-
*
211+
* Evaluate the given <a href="https://github.com/jayway/JsonPath">JsonPath</a>
212+
* expression against the response body and assert the resulting value with
213+
* the given Hamcrest {@link Matcher}, coercing the resulting value into the
214+
* given target type before applying the matcher.
215+
* <p>This can be useful for matching numbers reliably &mdash; for example,
216+
* to coerce an integer into a double.
213217
* @param expression the JSON path expression
214-
* @param targetClass the target class to coerce the matching type into.
215218
* @param matcher a matcher for the value expected at the JSON path
219+
* @param targetType the target type to coerce the matching value into
220+
* @since 5.2
221+
* @see #jsonPath(String, Object...)
222+
* @see #jsonPath(String, Matcher)
216223
*/
217-
public static <T> ResultMatcher jsonPath(String expression, Class<T> targetClass, Matcher<T> matcher) {
218-
return new JsonPathResultMatchers(expression).value(matcher, targetClass);
224+
public static <T> ResultMatcher jsonPath(String expression, Matcher<T> matcher, Class<T> targetType) {
225+
return new JsonPathResultMatchers(expression).value(matcher, targetType);
219226
}
220227

221228
/**

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

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

27-
private final long id;
28-
2927
public Person(String name) {
30-
this(0, name);
31-
}
32-
33-
public Person(long id, String name) {
3428
super(name);
35-
this.id = id;
36-
}
37-
38-
public long getId() {
39-
return id;
4029
}
4130

4231
}

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,6 @@ 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-
8470
@Test
8571
void getPerson99() throws Exception {
8672
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(id, "Dilbert");
34+
return new Person("Dilbert");
3535
}
36-
return new Person(id, "Wally");
36+
return new Person("Wally");
3737
}
3838

3939
}

spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ResponseBodyTests.java

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616

1717
package org.springframework.test.web.servlet.samples.standalone;
1818

19+
import javax.validation.constraints.NotNull;
20+
1921
import org.junit.Test;
2022

2123
import org.springframework.http.MediaType;
22-
import org.springframework.stereotype.Controller;
23-
import org.springframework.test.web.Person;
24+
import org.springframework.web.bind.annotation.GetMapping;
2425
import org.springframework.web.bind.annotation.PathVariable;
25-
import org.springframework.web.bind.annotation.RequestMapping;
26-
import org.springframework.web.bind.annotation.ResponseBody;
26+
import org.springframework.web.bind.annotation.RestController;
2727

28+
import static org.hamcrest.Matchers.equalTo;
2829
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
2930
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
3031
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
@@ -35,6 +36,7 @@
3536
* Response written from {@code @ResponseBody} method.
3637
*
3738
* @author Rossen Stoyanchev
39+
* @author Sam Brannen
3840
*/
3941
public class ResponseBodyTests {
4042

@@ -44,17 +46,49 @@ public void json() throws Exception {
4446
.perform(get("/person/Lee").accept(MediaType.APPLICATION_JSON))
4547
.andExpect(status().isOk())
4648
.andExpect(content().contentType("application/json"))
47-
.andExpect(jsonPath("$.name").value("Lee"));
49+
.andExpect(jsonPath("$.name").value("Lee"))
50+
.andExpect(jsonPath("$.age").value(42))
51+
.andExpect(jsonPath("$.age").value(42.0f))
52+
.andExpect(jsonPath("$.age").value(equalTo(42)))
53+
.andExpect(jsonPath("$.age").value(equalTo(42.0f), Float.class))
54+
.andExpect(jsonPath("$.age", equalTo(42)))
55+
.andExpect(jsonPath("$.age", equalTo(42.0f), Float.class));
4856
}
4957

5058

51-
@Controller
52-
private class PersonController {
59+
@RestController
60+
private static class PersonController {
5361

54-
@RequestMapping(value="/person/{name}")
55-
@ResponseBody
62+
@GetMapping("/person/{name}")
5663
public Person get(@PathVariable String name) {
57-
return new Person(name);
64+
Person person = new Person(name);
65+
person.setAge(42);
66+
return person;
67+
}
68+
}
69+
70+
@SuppressWarnings("unused")
71+
private static class Person {
72+
73+
@NotNull
74+
private final String name;
75+
76+
private int age;
77+
78+
public Person(String name) {
79+
this.name = name;
80+
}
81+
82+
public String getName() {
83+
return this.name;
84+
}
85+
86+
public int getAge() {
87+
return this.age;
88+
}
89+
90+
public void setAge(int age) {
91+
this.age = age;
5892
}
5993
}
6094

0 commit comments

Comments
 (0)