Testing with spring-test-mvc jsonpath returns null

徘徊边缘 提交于 2019-12-01 00:02:02

If you add the json path dependency to maven, or add the jar to your lib, then it will work. I think that Spring is not including the jsonPath dependency in the latest Spring 3.2.0 RC1 release. I'm guessing that this is the same for Spring-Test-MVC standalone project as well.

Here is the dependency for Maven:

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>0.8.1</version>
    <scope>test</scope>
</dependency>

You might also need the hamcrest library to use the jsonPath("$.test").value("test")

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-library</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>
Brett Robison

What does your json response body look like? You can see it by doing an .andDo(print())

You might want to try jsonPath("$.fName").

This is assuming that your json response is: {"fName":"first name"}

If your response is an array then you need jsonPath("$[0].fName") for a response like: [{"fName":"first name"},{"fName":"first name #2"}]

You can see more examples at: http://goessner.net/articles/JsonPath/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!