Testing with spring-test-mvc jsonpath returns null

后端 未结 2 918
不知归路
不知归路 2021-01-06 18:43

I am using Spring\'s \"spring-test-mvc\" library to test web controllers. I have a very simple controller that returns a JSON array. Then in my test I have:

         


        
2条回答
  •  囚心锁ツ
    2021-01-06 19:44

    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/

提交回复
热议问题