@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?