본문 바로가기
Project

[Toss] 계좌 해지(2)

by sangyunpark 2023. 8. 22.

계좌 해제가 잘 되는지 Test를 해보자

 

Controller Test code

@Test
    public void successDeleteAccount() throws Exception {

        //given
        given(accountService.deleteAccount(anyLong(), anyString()))
                .willReturn(
                        AccountDto.builder()
                                .userId(1L)
                                .accountNumber("1234567890")
                                .registeredAt(LocalDateTime.now())
                                .unregisteredAt(LocalDateTime.now())
                                .build()
                );
        
        //when
        
        //then
        mockMvc.perform(delete("/account")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(
                        new DeleteAccount.Request(1L, "1234567890")
                )))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.userId").value(1L))
                .andExpect(jsonPath("$.accountNumber").value("1234567890"))
                .andDo(print());
    }

mockMvc를 사용해서 http 요청을 한다.

 

 

Service Test Code

 

성공

@Test
    public void deleteAccountSuccess() throws Exception {

        AccountUser user = AccountUser.builder().id(12L).name("sy").build();

        //given
        given(accountUserRepository.findById(anyLong()))
                .willReturn(Optional.of(user));
        given(accountRepository.findByAccountNumber(anyString()))
                .willReturn(Optional.of(Account.builder().userId(12L).accountUser(user).accountNumber("1000000012").balance(0L).accountStatus(AccountStatus.IN_USE).build()));

        ArgumentCaptor<Account> captor = ArgumentCaptor.forClass(Account.class);

        //when
        AccountDto accountDto = accountService.deleteAccount(1L, "100000000");

        //then
        verify(accountRepository,times(1)).save(captor.capture());
        assertEquals(12L, accountDto.getUserId());
        assertEquals("1000000012", captor.getValue().getAccountNumber());
        assertEquals(AccountStatus.UNREGISTERED, captor.getValue().getAccountStatus());

    }

 

실패

(1) 해당 유저가 없는 경우

@Test
@DisplayName("해당 유저가 존재 x - 계좌 해지 실패 ")
  public void deleteAccount_UserNotFound() throws Exception {
      //given
      AccountUser user = AccountUser.builder()
              .id(12L)
              .name("sy")
              .build();

      given(accountUserRepository.findById(anyLong()))
              .willReturn(Optional.empty());

      //when
      AccountException exception = assertThrows(AccountException.class, () -> accountService.deleteAccount(1L, "1000000002"));
      // exception 발생

      //then
      assertEquals(ErrorCode.USER_NOT_FOUND, exception.getErrorCode()); // 에러 발생
    }

 

(2) 해당 계좌가 없는 경우

@Test
@DisplayName("해당 계좌가 존재 x - 계좌 해지 실패 ")
  public void deleteAccount_AccountNotFound() throws Exception {
      //given
      AccountUser user = AccountUser.builder()
              .id(12L)
              .name("sy")
              .build();

      given(accountUserRepository.findById(anyLong())).willReturn(Optional.of(user));
      given(accountRepository.findByAccountNumber(anyString())).willReturn(Optional.empty());

      //when
      AccountException exception = assertThrows(AccountException.class, () -> accountService.deleteAccount(1L, "1000000002"));
      // exception 발생

      //then
      assertEquals(ErrorCode.ACCOUNT_NOT_FOUND, exception.getErrorCode()); // 에러 발생
  }

 

(3) 계좌 소유주가 다른 경우

@Test
@DisplayName("계좌 소유주가 다른 경우 - 계좌 해지 실패 ")
   public void deleteAccount_UserAccountUnMatch() throws Exception {
       //given
       AccountUser user = AccountUser.builder()
                .id(12L)
                .name("sy")
                .build();

        AccountUser user2 = AccountUser.builder()
                .id(13L)
                .name("ys")
                .build();

        given(accountUserRepository.findById(anyLong())).willReturn(Optional.of(user));
        given(accountRepository.findByAccountNumber(anyString())).willReturn(Optional.of(Account.builder().accountUser(user2).accountNumber("1000000000").accountStatus(AccountStatus.IN_USE).balance(0L).build())); // 계좌의 유저 아이디 13

        //when
        AccountException exception = assertThrows(AccountException.class, () -> accountService.deleteAccount(1L, "1000000002"));
        // exception 발생

        //then
        assertEquals(ErrorCode.USER_ACCOUNT_UN_MATCH, exception.getErrorCode()); // 에러 발생
    }

(4) 계좌에 잔액이 있는 경우

@Test
    @DisplayName("계좌에 잔액이 있는 경우 - 계좌 해지 실패 ")
    public void deleteAccount_BalanceNotEmpty() throws Exception {
        //given
        AccountUser user = AccountUser.builder()
                .id(12L)
                .name("sy")
                .build();

        given(accountUserRepository.findById(anyLong())).willReturn(Optional.of(user));
        given(accountRepository.findByAccountNumber(anyString())).willReturn(Optional.of(Account.builder().accountUser(user).accountNumber("1000000000").accountStatus(AccountStatus.IN_USE).balance(1L).build())); // 계좌의 유저 아이디 13

        //when
        AccountException exception = assertThrows(AccountException.class, () -> accountService.deleteAccount(1L, "1000000002"));
        // exception 발생

        //then
        assertEquals(ErrorCode.BALANCE_NOT_EMPTY, exception.getErrorCode()); // 에러 발생
    }

(5) 계좌가 이미 해지인 경우

@Test
@DisplayName("계좌가 이미 해지인 경우 - 계좌 해지 실패 ")
    public void deleteAccount_Account_Already_Unregistered() throws Exception {
        //given
        AccountUser user = AccountUser.builder()
                .id(12L)
                .name("sy")
                .build();

        given(accountUserRepository.findById(anyLong())).willReturn(Optional.of(user));
        given(accountRepository.findByAccountNumber(anyString())).willReturn(Optional.of(Account.builder().accountUser(user).accountNumber("1000000000").accountStatus(AccountStatus.UNREGISTERED).balance(0L).build())); // 계좌의 유저 아이디 13

        //when
        AccountException exception = assertThrows(AccountException.class, () -> accountService.deleteAccount(1L, "1000000002"));
        // exception 발생

        //then
        assertEquals(ErrorCode.ACCOUNT_ALREADY_UNREGISTRED, exception.getErrorCode()); // 에러 발생
    }

 

'Project' 카테고리의 다른 글

[Toss] 계좌 확인(2)  (0) 2023.08.22
[Toss] 계좌 확인(1)  (0) 2023.08.22
[Toss] 계좌 해제(1)  (0) 2023.08.21
[Toss] 계좌 생성(4)  (0) 2023.08.21
[Toss] 계좌 생성(3)  (0) 2023.08.18