Skip to content
Advertisement

Junit test with jsonPath doesn’t find the matching content

I have this simple unit test:-

JavaScript

.andDo(MockMvcResultHandlers.print()); prints out this:-

JavaScript

I can see there is "city":"espoo". How do I match this. I tired:-

JavaScript

It give error:-

DEBUG org.springframework.test.web.servlet.TestDispatcherServlet – Completed 200 OK DEBUG com.jayway.jsonpath.internal.path.CompiledPath – Evaluating path: $.size() DEBUG com.jayway.jsonpath.internal.path.CompiledPath – Evaluating path: $[‘city’]

java.lang.AssertionError: No value at JSON path “$.city”

Advertisement

Answer

The response is of type Array:

JavaScript

Therefore you will need to choose the index first, then attribute $[0].city

JavaScript

=== Edited ===

Please try:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement