Support target type in MockMvcResultMatchers.jsonPath()
This commit introduces an overloaded jsonPath() method to specify a target type to coerce into for MockMvcResultMatchers. - jsonPath(String, Matcher<T>, Class<T>) Closes gh-23141
This commit is contained in:
committed by
Sam Brannen
parent
89ebdc766c
commit
72adc3d37e
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user