Junit Test Application-41-계좌이체 기능 JUnit 테스트

jaegeunsong97·2023년 8월 9일
0

Junit Bank Application 깃허브

Junit Bank Application 기록 노션

  • 서비스 테스트
@Transactional
     public AccountTransferResponseDto 계좌이체(AccountTransferRequestDto accountTransferRequestDto, Long userId) {
          // 1. 출금계좌 != 입금계좌
          if (accountTransferRequestDto.getWithdrawNumber().longValue() == accountTransferRequestDto.getDepositNumber()
                    .longValue()) {
               throw new CustomApiException("입출금계좌가 동일할 수 없습니다. ");
          }

          // 2. 0원 체크
          if (accountTransferRequestDto.getAmount() <= 0L) {
               throw new CustomApiException("0원 이하의 금액을 입금할 수 없습니다. ");
          }

          // 3. 출금계좌 확인
          Account withdrawAccountPS = accountRepository.findByNumber(accountTransferRequestDto.getWithdrawNumber())
                    .orElseThrow(
                              () -> new CustomApiException("출금계좌를 찾을 수 없습니다. "));

          // 4. 입금계좌 확인
          Account depositAccountPS = accountRepository.findByNumber(accountTransferRequestDto.getDepositNumber())
                    .orElseThrow(
                              () -> new CustomApiException("입금계좌를 찾을 수 없습니다. "));

          // 5. 출금 소유자 확인(로그인한 사람과 비교)
          withdrawAccountPS.checkOwner(userId);

          // 6. 출금 비밀번호 확인
          withdrawAccountPS.checkSamePassword(accountTransferRequestDto.getWithdrawPassword());

          // 7. 출금계좌 잔액 확인
          withdrawAccountPS.checkBalance(accountTransferRequestDto.getAmount());

          // 8. 이체하기
          withdrawAccountPS.withdraw(accountTransferRequestDto.getAmount());
          depositAccountPS.deposit(accountTransferRequestDto.getAmount());

          // 7. 거래내역 남기기
          Transaction transaction = Transaction.builder()
                    .withdrawAccount(withdrawAccountPS)
                    .depositAccount(depositAccountPS)
                    .withdrawAccountBalance(withdrawAccountPS.getBalance())
                    .depositAccountBalance(depositAccountPS.getBalance())
                    .amount(accountTransferRequestDto.getAmount())
                    .gubun(TransactionEnum.TRANSFER)
                    .sender(accountTransferRequestDto.getWithdrawNumber() + "")
                    .receiver(accountTransferRequestDto.getDepositNumber() + "")
                    .build();
          Transaction transactionPS = transactionRepository.save(transaction);

          // 8. DTO
          return new AccountTransferResponseDto(withdrawAccountPS, transactionPS);
     }
  • 컨트롤러 테스트
@WithUserDetails(value = "ssar", setupBefore = TestExecutionEvent.TEST_EXECUTION)
     @Test
     public void transferAccount_test() throws Exception {
          // given
          AccountTransferRequestDto accountTransferRequestDto = new AccountTransferRequestDto();
          accountTransferRequestDto.setWithdrawNumber(1111L);
          accountTransferRequestDto.setDepositNumber(2222L);
          accountTransferRequestDto.setWithdrawPassword(1234L);
          accountTransferRequestDto.setAmount(100L);
          accountTransferRequestDto.setGubun("TRANSFER");

          String requestBody = om.writeValueAsString(accountTransferRequestDto);
          System.out.println("테스트 : " + requestBody);

          // when
          ResultActions resultActions = mvc
                    .perform(post("/api/s/account/transfer").content(requestBody)
                              .contentType(MediaType.APPLICATION_JSON));
          String responseBody = resultActions.andReturn().getResponse().getContentAsString();
          System.out.println("테스트 : " + responseBody);

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

만약 테스트할 때 400이 뜬다면 ResponseDto 가서 @JsonIgnore가 값을 받는지 확인

본인은 400이 계속 나와서 뭐 떄문이지? 하다가 30분 삽질해서 위치 찾음 ㅠㅠ

this. = 가 없어서 400이 뜨는 것

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

0개의 댓글