-
Notifications
You must be signed in to change notification settings - Fork 38.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to specify JsonProvider
when making MVC jsonPath()
assertion
#31423
Comments
I suggest add a |
Related issue: json-path/JsonPath#918 |
I found https://github.com/lukas-krecan/JsonUnit might helps me. |
JsonProvider
when making MVC jsonPath()
assertion
Hi @DevDengChao,
Are you saying the default Or are you saying that Spring's Or are you saying something else? If so, please expound. Thanks |
Yes, there is no helper methods I can use to assert something is a json array which contains certain elements. |
Even one of the JsonProvider implementation has such api, I can not switch between implementations jsonPath() uses now. |
You are free to use any core Hamcrest matcher as well as any matchers from third-party libraries or matchers that you create yourself. For the specific use case you've outlined, the following test class demonstrates that it's possible with built-in matchers from Hamcrest. import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static org.hamcrest.CoreMatchers.either;
import static org.hamcrest.CoreMatchers.everyItem;
import static org.hamcrest.CoreMatchers.is;
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;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
class JsonPathArrayTests {
private MockMvc mockMvc= standaloneSetup(new PersonController())
.defaultRequest(get("/").accept(MediaType.APPLICATION_JSON))
.alwaysExpect(status().isOk())
.alwaysExpect(content().contentType("application/json"))
.build();
@Test
void getPerson() throws Exception {
String phoneNumberTypes = "$.phoneNumbers[*].type";
mockMvc.perform(get("/person"))
.andExpect(jsonPath(phoneNumberTypes).isArray())
.andExpect(jsonPath(phoneNumberTypes).value(everyItem(either(is("iPhone")).or(is("home")))));
}
@RestController
private static class PersonController {
@RequestMapping("/person")
String get() {
return """
{
"firstName": "John",
"lastName": "doe",
"age": 26,
"address": {
"streetAddress": "naist street",
"city": "Nara",
"postalCode": "630-0192"
},
"phoneNumbers": [
{
"type": "iPhone",
"number": "0123-4567-8888"
},
{
"type": "home",
"number": "0123-4567-8910"
}
]
}
""";
}
}
} |
I agree that it could be useful to provide a custom (or non-default) |
@sbrannen Thanks for your code snippet, I should dig Hamcrest more before opening this issue.
As I metioned in netplex/json-smart-v2#162 (comment), something like https://github.com/Crunc/hamcrest-json-matchers provides some helper methods which could make assertions on json array, but it dosen't compatible with JsonSmart json array, if I could switch to other JsonProvider implementations, then I can choose to use such libs instead of write a long chain of Hamcrest matchers myself. |
I've created #31651 to open up |
I was trying to make an assertion like something above, but there is no proper matcher helpers currently for the default
JsonProvider
(JsonSmartJsonProvider
).I opened netplex/json-smart-v2#162 to asking for
JsonSmartMatchers
as I cannot change whatJsonProvider
jsonPath() uses now.This prevents me from choosing other
JsonProvider
who might have matcher helpers already.The text was updated successfully, but these errors were encountered: