netplex / json-smart-v2

Apache License 2.0
151 stars 68 forks source link

Lacking org.hamcrest.Matcher helpers to perform assertion in unit tests #162

Open DevDengChao opened 1 year ago

DevDengChao commented 1 year ago

Story

Spring MVC's MockMvcResultMatchers#jsonPath() uses com.jayway.jsonpath.JsonPath to evaluate string content into objects, and JsonPath uses JsonSmartJsonProvider by default, when a developer attempt to do an unit test like below, there is no suitable helper methods he/she can use:

/*
{
    "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"
        }
    ]
}
*/
mockMvc.perform(get("/user/John"))
    .andExpect(jsonPath("$.phoneNumbers[*].type").value( ??? ));

Expected solution

Provide some org.hamcrest.Matcher helpers like Matchers.arrayContaining() ?

Related documents

DevDengChao commented 1 year ago

I think there could be a net.minidev:json-smart-matchers:<version> package, and encourage downstream testing packages to include it by default.

shoothzj commented 1 year ago

Hello, @DevDengChao According to the context, I think methods and ability should be add in the JsonProvider interface If SpringMvc still uses JsonPath?

Or say you want SringMvc directly uses JsonSmart, and we add these abilities to JsonSmart?

DevDengChao commented 1 year ago

I think methods and ability should be add in the JsonProvider interface If SpringMvc still uses JsonPath

:+1:

Then here comes a question, should JsonPath be responsible to implement matchers for all json providers itself or it is only responsible to delegate requests, eg: from JsonPathMatchers.arrayContaining() to JsonSmartMatchers.arrayContaining() 🤔

DevDengChao commented 1 year ago

For me, I think JsonSmart should implement its matchers itself. Then I can use JsonSmartMatchers in unit test cases before JsonPathMatchers to adapt all providers' implementation.

DevDengChao commented 1 year ago

I've found a existing json matchers facade https://github.com/Crunc/hamcrest-json-matchers , but it does not support JsonSmart now, nor support making assertion on json array element. 😢