I'm testing for a controller but my test code is no value at JSON path.
It's my test code.
@Test
@WithMockCustomUser
void When_ValidBoardIsUsedToCreateBoard_Then_WillSuccess() {
BoardDTO boardDTO = BoardDTO.builder().title("test").description("test").build();
AuthenticatedUser authenticatedUser = (AuthenticatedUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
Mono<BoardDTO> boardDTOMono = Mono.just(BoardDTO.builder()
.id(1L).title("test").description("test").authorId(1L)
.build());
// It failed.
Mockito.when(boardService.saveBoard(boardDTO, authenticatedUser))
.thenReturn(boardDTOMono);
// It succeeded.
// Mockito.when(boardService.saveBoard(any(BoardDTO.class), any(AuthenticatedUser.class)))
// .thenReturn(boardDTOMono);
webTestClient.post().uri("/api/saveBoard")
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(boardDTO))
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.title").isEqualTo("test");
}
and it's my controller's code
@PostMapping("/api/saveBoard")
public Publisher<BoardDTO> saveBoard(@RequestBody BoardDTO boardDTO,
@AuthenticationPrincipal AuthenticatedUser authenticatedUser) {
return boardService.saveBoard(boardDTO, authenticatedUser);
}
The test results in an error.
java.lang.AssertionError: No value at JSON path "$.title"
at org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:304)
at org.springframework.test.util.JsonPathExpectationsHelper.assertValue(JsonPathExpectationsHelper.java:99)
at org.springframework.test.web.reactive.server.JsonPathAssertions.isEqualTo(JsonPathAssertions.java:54)
at com.reactive.tikwon.controllers.BoardControllerTest.Given_UserIsAuthenticated_When_ValidBoardNameAndBoardContentAreUsedToCreateBoard_Then_SavingWillSuccess(BoardControllerTest.java:80)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
Caused by: com.jayway.jsonpath.PathNotFoundException: Expected to find an object with property ['title'] in path $ but found 'net.minidev.json.JSONArray'. This is not a json object according to the JsonProvider: 'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'.
I want to succeed without any(). Why is it failing without any()? And how can I succeed the test without any()?
I need your helps, please.