Polish contribution
See gh-23141
This commit is contained in:
@@ -187,35 +187,42 @@ public abstract class MockMvcResultMatchers {
|
|||||||
* {@link String#format(String, Object...)}.
|
* {@link String#format(String, Object...)}.
|
||||||
* @param expression the JSON path expression, optionally parameterized with arguments
|
* @param expression the JSON path expression, optionally parameterized with arguments
|
||||||
* @param args arguments to parameterize the JSON path expression with
|
* @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) {
|
public static JsonPathResultMatchers jsonPath(String expression, Object... args) {
|
||||||
return new JsonPathResultMatchers(expression, args);
|
return new JsonPathResultMatchers(expression, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Access to response body assertions using a
|
* Evaluate the given <a href="https://github.com/jayway/JsonPath">JsonPath</a>
|
||||||
* <a href="https://github.com/jayway/JsonPath">JsonPath</a> expression
|
* expression against the response body and assert the resulting value with
|
||||||
* to inspect a specific subset of the body and a Hamcrest matcher for
|
* the given Hamcrest {@link Matcher}.
|
||||||
* asserting the value found at the JSON path.
|
|
||||||
* @param expression the JSON path expression
|
* @param expression the JSON path expression
|
||||||
* @param matcher a matcher for the value expected at the JSON path
|
* @param matcher a matcher for the value expected at the JSON path
|
||||||
|
* @see #jsonPath(String, Object...)
|
||||||
|
* @see #jsonPath(String, Matcher, Class)
|
||||||
*/
|
*/
|
||||||
public static <T> ResultMatcher jsonPath(String expression, Matcher<T> matcher) {
|
public static <T> ResultMatcher jsonPath(String expression, Matcher<T> matcher) {
|
||||||
return new JsonPathResultMatchers(expression).value(matcher);
|
return new JsonPathResultMatchers(expression).value(matcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An overloaded variant of {@link #jsonPath(String, Matcher)} (Matcher)} that also accepts
|
* Evaluate the given <a href="https://github.com/jayway/JsonPath">JsonPath</a>
|
||||||
* a target type for the resulting value that the matcher can work reliably against.
|
* expression against the response body and assert the resulting value with
|
||||||
* <p> This can be useful for matching numbers reliably — for example,
|
* the given Hamcrest {@link Matcher}, coercing the resulting value into the
|
||||||
* to coerce an integer into a double.</p>
|
* given target type before applying the matcher.
|
||||||
*
|
* <p>This can be useful for matching numbers reliably — for example,
|
||||||
|
* to coerce an integer into a double.
|
||||||
* @param expression the JSON path expression
|
* @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 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 <T> ResultMatcher jsonPath(String expression, Class<T> targetClass, Matcher<T> matcher) {
|
public static <T> ResultMatcher jsonPath(String expression, Matcher<T> matcher, Class<T> targetType) {
|
||||||
return new JsonPathResultMatchers(expression).value(matcher, targetClass);
|
return new JsonPathResultMatchers(expression).value(matcher, targetType);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -24,19 +24,8 @@ package org.springframework.test.context.junit.jupiter.comics;
|
|||||||
*/
|
*/
|
||||||
public class Person extends Character {
|
public class Person extends Character {
|
||||||
|
|
||||||
private final long id;
|
|
||||||
|
|
||||||
public Person(String name) {
|
public Person(String name) {
|
||||||
this(0, name);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Person(long id, String name) {
|
|
||||||
super(name);
|
super(name);
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,20 +67,6 @@ class MultipleWebRequestsSpringExtensionTests {
|
|||||||
.andExpect(jsonPath("$.name", is("Dilbert")));
|
.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
|
@Test
|
||||||
void getPerson99() throws Exception {
|
void getPerson99() throws Exception {
|
||||||
this.mockMvc.perform(get("/person/99").accept(MediaType.APPLICATION_JSON))
|
this.mockMvc.perform(get("/person/99").accept(MediaType.APPLICATION_JSON))
|
||||||
|
|||||||
@@ -31,9 +31,9 @@ class PersonController {
|
|||||||
@GetMapping("/person/{id}")
|
@GetMapping("/person/{id}")
|
||||||
Person getPerson(@PathVariable long id) {
|
Person getPerson(@PathVariable long id) {
|
||||||
if (id == 42) {
|
if (id == 42) {
|
||||||
return new Person(id, "Dilbert");
|
return new Person("Dilbert");
|
||||||
}
|
}
|
||||||
return new Person(id, "Wally");
|
return new Person("Wally");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,15 +16,16 @@
|
|||||||
|
|
||||||
package org.springframework.test.web.servlet.samples.standalone;
|
package org.springframework.test.web.servlet.samples.standalone;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.test.web.Person;
|
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
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.content;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
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.
|
* Response written from {@code @ResponseBody} method.
|
||||||
*
|
*
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
|
* @author Sam Brannen
|
||||||
*/
|
*/
|
||||||
public class ResponseBodyTests {
|
public class ResponseBodyTests {
|
||||||
|
|
||||||
@@ -44,17 +46,49 @@ public class ResponseBodyTests {
|
|||||||
.perform(get("/person/Lee").accept(MediaType.APPLICATION_JSON))
|
.perform(get("/person/Lee").accept(MediaType.APPLICATION_JSON))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(content().contentType("application/json"))
|
.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
|
@RestController
|
||||||
private class PersonController {
|
private static class PersonController {
|
||||||
|
|
||||||
@RequestMapping(value="/person/{name}")
|
@GetMapping("/person/{name}")
|
||||||
@ResponseBody
|
|
||||||
public Person get(@PathVariable String 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user