// 입금 트랜젝션 -> 계좌 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);
}
계좌입금 서비스 테스트에 대한 고찰
다른 부가적인 것들은 중요한 테스트가 아님.
결론은 목적에 맞게 테스트를 해야하는 것
@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 조절
}