카테고리 없음

[내일배움캠프] 2월 22일 수요일 TIL 회고록

tft4rollz 2023. 2. 22. 23:05

오늘 한 것 : 테스트 코드 작성

 

Mockito를 사용하여 강의자료에 있는 영상을 보며 따라 만들었는데 전부 실패가 나온다..ㅠㅠ

 

유저가 쓴 댓글 전체 조회 테스트 코드를 만들어보았는데 익셉션이 뜨면서 실패가 된다..

몇시간동안 고쳐봤는데 도저히 답이 없다..

@Test
void getMyComments() {
  // given
  CommentList commentList = new CommentList();
  Long userId = 1L;
  User user = new User("user2", passwordEncoder.encode("pass12!@"),
      "nickname2", "aa@b.com");
  when(commentRepository.findAllByUserId(commentList.toPageable(), user.getId())).thenReturn(
      Page.empty());
  // when
  Page<CommentResponse> responses = commentService.getMyComments(commentList, userId, user);
  // then
  assertThat(responses).isEmpty();

  // verify
  verify(commentRepository).findAllByUserId(commentList.toPageable(), user.getId());
}

 

또 질문 댓글 수정하는 기능도 테스트 코드를 만들어 보았는데 이거도 오류가 뜨면서 안된다.

각종 오류들이 다나와서 파파고로 변역해보면서 고쳐봤는데 쉽지 않다.. 이거만 6시간 수정한 것 같다..

@Test
void updateQuestionComment() {
  // given
  CommentRequest commentRequest = CommentRequest.builder()
      .content("댓글수정")
      .build();
  User user = new User("user2", passwordEncoder.encode("pass12!@"),
      "nickname2", "aa@b.com");
  Long questionBoardId = 1L;
  Long userId = 2L;
  Comment comment = new Comment(user, commentRequest, questionBoardId);
  commentRepository.save(comment);
  when(commentRepository.findById(anyLong())).thenReturn(Optional.of(comment));
  when(userService.getProfile(anyLong())).thenReturn(isNotNull());
  when(commentRepository.findByUserId(userId)).thenReturn(Optional.of(comment));

  // when
  commentService.getNicknameByComment(comment);
  commentService.updateQuestionComment(anyLong(), commentRequest, user);

  // then
  verify(commentRepository, times(1)).save(any(Comment.class));
}

진짜 미쳐버릴거같다 ㅋㅋㅋㅋ 테스트코드 작성하기 너무 힘들다... 너무 지친다.. 내일 이어서 고쳐봐야겠다..