-
BDDMokito.given return nullBackEnd/스프링 2022. 7. 29. 16:31
Controller 에서는 BoardRequest 를 받고, 서비스는 Board 를 받아 Mocking 할 때 특정 Board 를 생성해 given 인자로 사용하면
항상 null이 반환된다.
테스트 코드에서 들어가는 request 와 서비스 Mocking 에 인자는 다른 객체이기 때문에 any 로 타입명시를 해줘 해결했다.
given(boardService.save(any(Board.class) )).willReturn(board);
String body = objectMapper.writeValueAsString(boardRequest); mockMvc.perform(post("/api/boards") .content(body) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andDo(restDocs.document( requestHeaders( headerWithName(HttpHeaders.CONTENT_TYPE).description(MediaType.MULTIPART_FORM_DATA) ), requestFields( fieldWithPath("title").description("제목").type(JsonFieldType.STRING), fieldWithPath("content").description("내용").type(JsonFieldType.STRING), fieldWithPath("status").description("공개여부").type(JsonFieldType.STRING), fieldWithPath("memberId").description("회원ID").type(JsonFieldType.NUMBER), fieldWithPath("files").optional().description("첨부파일"), fieldWithPath("categoryId").description("카테고리ID").type(JsonFieldType.NUMBER) ), responseHeaders( ), responseFields( fieldWithPath("id").description("아이디").type(JsonFieldType.NUMBER), fieldWithPath("title").description("제목").type(JsonFieldType.STRING), fieldWithPath("content").description("내용").type(JsonFieldType.STRING), fieldWithPath("categoryName").description("카테고리명").type(JsonFieldType.STRING), fieldWithPath("email").description("이메일").type(JsonFieldType.STRING), fieldWithPath("viewCount").description("조회수").type(JsonFieldType.NUMBER) ) ));
반응형'BackEnd > 스프링' 카테고리의 다른 글
Controller Test - RequestDTO with MultipartFile (0) 2022.08.01 @Valid 위치 (0) 2022.07.29 @AuthenticationPrincipal null (0) 2022.07.28 @Builder 위치와 연관관계 세팅 (0) 2022.07.24 @Value 는 객체 bean 등록 필수, static 키워드 사용 x (0) 2022.07.23