How to use MockMvcResultMatchers.jsonPath

We Are Going To Discuss About How to use MockMvcResultMatchers.jsonPath. So lets Start this Java Article.

How to use MockMvcResultMatchers.jsonPath

  1. How to use MockMvcResultMatchers.jsonPath

    Your code works for me just fine. I am using Jackson to parse the cases and that's probably the only difference.

  2. use MockMvcResultMatchers.jsonPath

    Your code works for me just fine. I am using Jackson to parse the cases and that's probably the only difference.

Solution 1

I think you need to pass the Matcher to the jsonPath method. For example.

.andExpect(jsonPath("$.version", is("0.1")))

References:
http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/test/web/servlet/result/MockMvcResultMatchers.html#jsonPath(java.lang.String,%20org.hamcrest.Matcher)

https://www.petrikainulainen.net/programming/spring-framework/integration-testing-of-spring-mvc-applications-write-clean-assertions-with-jsonpath/

SpringMVC/ mockMVC/ jsonpath compare list of strings

Original Author Jose Martinez Of This Content

Solution 2

Your code works for me just fine. I am using Jackson to parse the cases and that’s probably the only difference. Check my code:

Result class:

public class Res {
    String version;

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }
}

Controller:

@RequestMapping(value = "/blah",
        method = GET,
        produces = APPLICATION_JSON_VALUE)
public HttpEntity<Res> doIt() {
    Res res = new Res();
    res.setVersion("0.1");
    return new HttpEntity<>(res);
}

Test:

@Test
public void blahTest() throws Exception {
    this.mockMvc.perform(
            MockMvcRequestBuilders.get("/blah"))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
            .andExpect(jsonPath("$.version").value("0.1"))
            .andDo(MockMvcResultHandlers.print());
}

Response:

MockHttpServletResponse:
           Status = 200
    Error message = null
          Headers = {Content-Type=[application/json;charset=UTF-8]}
     Content type = application/json;charset=UTF-8
             Body = {"version":"0.1"}
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

I can only recommend trying to change the Json parser library you are using. Otherwise, try to update your code with all the parts that are needed to create a simple, minimal and reproducible example of your problem.

Original Author MartinTeeVarga Of This Content

Solution 3

All credit for this answer goes to @pramodh (Hidden in OP’s comments)

I had the exact same problem and installing his reccomended dependencies fixed everything.

<dependency>
    <groupId>net.minidev</groupId>
    <artifactId>json-smart</artifactId>
    <version>2.3</version>
    <scope>test</scope>
</dependency> 
<dependency> 
    <groupId>net.minidev</groupId>
    <artifactId>asm</artifactId>
    <version>1.0.2</version>
    <scope>test</scope>
</dependency>

Original Author coltonfranco Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment