Skip to content
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

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

Open
DevDengChao opened this issue Oct 11, 2023 · 5 comments
Open

Comments

@DevDengChao
Copy link

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
Copy link
Author

DevDengChao commented Oct 11, 2023

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

@hezhangjian
Copy link
Collaborator

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
Copy link
Author

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

👍

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
Copy link
Author

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
Copy link
Author

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. 😢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants