Junit Test Application-31-본인계좌목록보기 JUnit 테스트

jaegeunsong97·2023년 8월 7일
0

Junit Bank Application 깃허브

Junit Bank Application 기록 노션

@Test
     public void 계좌목록보기_유저별_test() throws Exception {
          // given
          Long userId = 1L;

          // stub 1
          User ssar = newMockUser(userId, "ssar", "쌀");
          when(userRepository.findById(userId)).thenReturn(Optional.of(ssar));

          // stub 2
          Account ssarAccount1 = newMockAccount(1L, 1111L, 1000L, ssar);
          Account ssarAccount2 = newMockAccount(2L, 2222L, 1000L, ssar);
          List<Account> accountList = Arrays.asList(ssarAccount1, ssarAccount2);
          when(accountRepository.findByUser_id(any())).thenReturn(accountList);

          // when
          AccountListResponseDto accountListResponseDto = accountService.계좌목록보기_유저별(userId);

          // then
          assertThat(accountListResponseDto.getFullname()).isEqualTo("쌀");
          assertThat(accountListResponseDto.getAccounts().size()).isEqualTo(2);
     }

어차피 Repo 테스트가 아니니까 stub을 2개를 걸어준다.

  • 컨트롤러 테스트
@WithUserDetails(value = "ssar", setupBefore = TestExecutionEvent.TEST_EXECUTION)
     @Test
     public void findUserAccount_test() throws Exception {
          // given

          // when
          ResultActions resultActions = mvc.perform(get("/api/s/account/login-user"));
          String responseBody = resultActions.andReturn().getResponse().getContentAsString();
          System.out.println("테스트 : " + responseBody);

          // then
          resultActions.andExpect(status().isOk());
     }

본 코드를 보면 파라미터값이 @AuthenticationPrincipal LoginUser loginUser 이기때문에 아무것도 존재하지 않는다.

profile
블로그 이전 : https://medium.com/@jaegeunsong97

0개의 댓글