Search code examples
javajsonjunitspring-mvc-test

Assertion error: No value for JSON Path in JUnit test


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.


Solution

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

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