17

I have written a test and it succeeded before but now I get an AssertionError: No value for JSON Path.

@Test
public void testCreate() throws Exception {
    Wine wine = new Wine();
    wine.setName("Bordeaux");
    wine.setCost(BigDecimal.valueOf(10.55));

    new Expectations() {
        {
            wineService.create((WineDTO) any);
            result = wine;
        }
    };

    MockMultipartFile jsonFile = new MockMultipartFile("form", "", "application/json", "{\"name\":\"Bordeaux\", \"cost\": \"10.55\"}".getBytes());
    this.webClient.perform(MockMvcRequestBuilders.fileUpload("/wine").file(jsonFile))
            .andExpect(MockMvcResultMatchers.status().is(200))
            .andExpect(MockMvcResultMatchers.jsonPath("$.name").value("Bordeaux"))
            .andExpect(MockMvcResultMatchers.jsonPath("$.cost").value(10.55));
}

The error I get is:

java.lang.AssertionError: No value for JSON path: $.name, exception: No results path for $['name']

I don't understand what it is not getting or what is missing.

Mathias Dpunkt
  • 11,594
  • 4
  • 45
  • 70
Robin van Aalst
  • 217
  • 1
  • 2
  • 7

6 Answers6

10

I was getting same problem.

Solution :

Use .andReturn().getResponse().getContentAsString();, your response will be a string. My response was:

{"url":null,"status":200,"data":{"id":1,"contractName":"Test contract"}

When I was trying to do .andExpect(jsonPath("$.id", is(1))); there was an error: java.lang.AssertionError: No value for JSON path: $.id

To fix it, I did .andExpect(jsonPath("$.data.id", is(1))); and it works because id is a field in data.

Javier C.
  • 7,859
  • 5
  • 41
  • 53
Nagesh Dalave
  • 109
  • 1
  • 4
9

You are asserting that your response contains a field name with value Bordeaux.

You can print your response using this.webClient.perform(...).andDo(print()).

Mathias Dpunkt
  • 11,594
  • 4
  • 45
  • 70
4

Most likely jsonPath interprets the body of your file as a list and this should do the trick (mind the added square brackets as list accessors):

.andExpect(MockMvcResultMatchers.jsonPath("$[0].name").value("Bordeaux"))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].cost").value(10.55));
1

I had the same problem after adding jackson-dataformat-xml dependency to my project.

To solve this i had to change my response entity from:

return new ResponseEntity<>(body, HttpStatus.*STATUS*)

to

return ResponseEntity.*status*().contentType(MediaType.APPLICATION_JSON).body(*your body*).

In this way it will work fine, cause you've directly set the json as return type for your body.

Yurii Malskiy
  • 23
  • 1
  • 5
1

My return body was {'status': 'FINISHED', 'active':false} and jsonPath did see status field, but saw active. The solution: use jsonPath("$.['status']") instead of jsonPath("$.status")

My assumption: Probably jsonPath ignores some keywords like 'status', etc....

ouflak
  • 2,458
  • 10
  • 44
  • 49
-3

Whatever you are testing for .name does not have a property called name anymore the error message is pretty clear about that part.

java.lang.AssertionError: No value for JSON path: $.name, exception: No results path for $['name']

No one but you knows what you changed to make it go from working to not working nothing you posted in your question can tell us that.