0
@DisplayName("test getCarportById(), Status OK")
@Test
void testGetCarportLocationById() throws Exception {

    // given
    final String externalId = "123L";

    final CarportLocation carportLocation = CarportLocationMockHelper.createCarportLocationByIdDTO(externalId);

    given(carportService.getCarportLocationToId(externalId.toString())).willReturn(carportLocation);

    // when

    final MvcResult mvcResult = this.mockMvc.perform(getRequestBuilderMockCarportLocationsById(externalId.toString()))
                                            .andExpect(status().isOk())
                                            .andDo(print())
                                            .andExpect(content().contentType("application/json"))
                                            .andExpect(resultMatcherOK)
                                            .andExpect(MockMvcResultMatchers.jsonPath("$.externalId")
                                                                            .value(CarportLocation.getExternalId()))
                                            .andReturn();

    // then

    assertEquals("application/json", mvcResult.getResponse()
                                              .getContentType());
}
public static CarportLocation createCarportLocationByIdDTO(String externalId) {

    finalCarportLocation carportLocation = new CarportLocation();
    carportLocation.setId(123456L);
    carportLocation.setExternalId(externalId);
    carportLocation.setCarportName("Carport1");
    carportLocation.setLocX(12.4534);
    carportLocation.setLocY(19.5678);
    carportLocation.setLocZ(25.0);

    return carportLocation;
}

This is the expression I print out by .andDo(print()):

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /carport/carports/filter
       Parameters = {carportNameAndExternalId=[123L]}
          Headers = []
             Body = <no character encoding set>
    Session Attrs = {}

Handler:
             Type = controller.CarportController
           Method = controller.CarportController#_getCarportLocationsByIdAndName(String)

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 200
    Error message = null
          Headers = [Content-Type:"application/json"]
     Content type = application/json
             Body = []
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

Only unfortunately my JUnit test fails and I am not sure why.

java.lang.AssertionError: No value at JSON path "$.externalId"

Of course, I have also already searched for the problem and came across the following SO question: Assertion error: No value for JSON Path in JUnit test

only unfortunately a "$[1].externalId" did not help me. The andDo(print()) method has also therefore but can't really work with the output. Does anyone know how I have to adjust the json path so that my test is successful?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Doncarlito87
  • 359
  • 1
  • 8
  • 25

1 Answers1

0

According to the below:

MockHttpServletResponse:
           Status = 200
    Error message = null
          Headers = [Content-Type:"application/json"]
     Content type = application/json
             Body = []
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

The response body is empty. There is no JSON response being returned, so how do you expect "$.externalId" to be found?

Also the format should be:

.andExpect(jsonPath("$.externalId", is(CarportLocation.getExternalId())))
JCompetence
  • 6,997
  • 3
  • 19
  • 26