Junit Test Application-37-계좌입금 기능 JUnit 테스트

jaegeunsong97·2023년 8월 8일
0

Junit Bank Application 깃허브

Junit Bank Application 기록 노션

  • 더미 데이터
// 입금 트랜젝션 -> 계좌 1100원 변경 -> 입금 트랜젝션 히스토리가 생성되어야 함.
     protected static Transaction newMockDepositTransaction(Long id, Account account) {
          account.deposit(100L);
          return Transaction.builder()
                    .id(id)
                    .depositAccount(account)
                    .withdrawAccount(null)
                    .depositAccountBalance(account.getBalance())
                    .withdrawAccountBalance(null)
                    .amount(100L)
                    .gubun(TransactionEnum.DEPOSIT)
                    .sender("ATM")
                    .receiver(account.getNumber() + "")
                    .tel("01088887777")
                    .createAt(LocalDateTime.now())
                    .updateAt(LocalDateTime.now())
                    .build();
     }
  • 서비스 테스트 코드
@Test
     public void 계좌입금_test() throws Exception {
          // given
          AccountDepositRequestDto accountDepositRequestDto = new AccountDepositRequestDto();
          accountDepositRequestDto.setNumber(1111L);
          accountDepositRequestDto.setAmount(100L);
          accountDepositRequestDto.setGubun("DEPOSIT");
          accountDepositRequestDto.setTel("01088887777");

          // stub 1
          User ssar = newMockUser(1L, "ssar", "쌀");
          Account ssarAccount1 = newMockAccount(1L, 1111L, 1000L, ssar);
          when(accountRepository.findByNumber(any())).thenReturn(Optional.of(ssarAccount1));

          // stub 2
          Transaction transaction = newMockDepositTransaction(1L, ssarAccount1);
          when(transactionRepository.save(any())).thenReturn(transaction);

          // when
          AccountDepositResponseDto accountDepositResponseDto = accountService.계좌입금(accountDepositRequestDto);
          System.out.println(
                    "테스트 : 트랜젝션 입금계좌 잔액 : " + accountDepositResponseDto.getTransaction().getDepositAccountBalance());
          System.out.println("테스트 : 계좌쪽 잔액 : " + ssarAccount1.getBalance());

          // then

     }

이유 생각

테스트 할때는 꼬이기 때문에 그냥 각각의 stub을 작성할 때는 새로 만들자

즉, 스텁이 진행될 때마다 연관된 객체는 새로 만들어서 주입하기 - 타이밍 때문에 꼬인다.

@Test
     public void 계좌입금_test() throws Exception {
          // given
          AccountDepositRequestDto accountDepositRequestDto = new AccountDepositRequestDto();
          accountDepositRequestDto.setNumber(1111L);
          accountDepositRequestDto.setAmount(100L);
          accountDepositRequestDto.setGubun("DEPOSIT");
          accountDepositRequestDto.setTel("01088887777");

          // stub 1
          User ssar = newMockUser(1L, "ssar", "쌀");
          Account ssarAccount1 = newMockAccount(1L, 1111L, 1000L, ssar);
          when(accountRepository.findByNumber(any())).thenReturn(Optional.of(ssarAccount1));

          // stub 2
          Account ssarAccount2 = newMockAccount(1L, 1111L, 1000L, ssar);
          Transaction transaction = newMockDepositTransaction(1L, ssarAccount2);
          when(transactionRepository.save(any())).thenReturn(transaction);

          // when
          AccountDepositResponseDto accountDepositResponseDto = accountService.계좌입금(accountDepositRequestDto);
          System.out.println(
                    "테스트 : 트랜젝션 입금계좌 잔액 : " + accountDepositResponseDto.getTransaction().getDepositAccountBalance());
          System.out.println("테스트 : 계좌쪽 잔액 : " + ssarAccount1.getBalance());

          // then

     }

완성 코드

@Test
     public void 계좌입금_test() throws Exception {
          // given
          AccountDepositRequestDto accountDepositRequestDto = new AccountDepositRequestDto();
          accountDepositRequestDto.setNumber(1111L);
          accountDepositRequestDto.setAmount(100L);
          accountDepositRequestDto.setGubun("DEPOSIT");
          accountDepositRequestDto.setTel("01088887777");

          // stub 1
          User ssar = newMockUser(1L, "ssar", "쌀");
          Account ssarAccount1 = newMockAccount(1L, 1111L, 1000L, ssar);
          when(accountRepository.findByNumber(any())).thenReturn(Optional.of(ssarAccount1));

          // stub 2
          Account ssarAccount2 = newMockAccount(1L, 1111L, 1000L, ssar);
          Transaction transaction = newMockDepositTransaction(1L, ssarAccount2);
          when(transactionRepository.save(any())).thenReturn(transaction);

          // when
          AccountDepositResponseDto accountDepositResponseDto = accountService.계좌입금(accountDepositRequestDto);
          System.out.println(
                    "테스트 : 트랜젝션 입금계좌 잔액 : " + accountDepositResponseDto.getTransaction().getDepositAccountBalance());
          System.out.println("테스트 : 계좌쪽 잔액 : " + ssarAccount1.getBalance());
          System.out.println("테스트 : 계좌쪽 잔액 : " + ssarAccount2.getBalance());

          // then
          assertThat(ssarAccount1.getBalance()).isEqualTo(1100L);
          assertThat(accountDepositResponseDto.getTransaction().getDepositAccountBalance()).isEqualTo(1100L);
     }

계좌입금 서비스 테스트에 대한 고찰

  1. 0원 체크
  2. deposit 되나?
  3. dto 만들어지나?

다른 부가적인 것들은 중요한 테스트가 아님.

  • 0원 체크, 잘 작동

결론은 목적에 맞게 테스트를 해야하는 것


  • 컨트롤러 테스트 코드
@Test
     public void depositAccount_test() throws Exception {
          // given
          AccountDepositRequestDto accountDepositRequestDto = new AccountDepositRequestDto();
          accountDepositRequestDto.setNumber(1111L);
          accountDepositRequestDto.setAmount(100L);
          accountDepositRequestDto.setGubun("DEPOSIT");
          accountDepositRequestDto.setTel("01088887777");

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

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

          // then
          resultActions.andExpect(status().isCreated()); // @JsonIgnore 조절
     }

반드시 각 레이어 단에 맞는 검증을 하자

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

0개의 댓글