본문 바로가기
Project

[Toss] 계좌 확인(2)

by sangyunpark 2023. 8. 22.

기능을 만들었으니 테스트를 해보자

 

Controller Test

@Test
public void successGetAccountByUserId() throws Exception {

    //given
    List<AccountDto> accountDtos = Arrays.asList(
            AccountDto.builder()
                    .accountNumber("1234567890")
                    .balance(1000L)
                    .build(),
            AccountDto.builder()
                    .accountNumber("1111111111")
                    .balance(1000L)
                    .build(),
            AccountDto.builder()
                    .accountNumber("2222222222")
                    .balance(1000L)
                    .build()
            );

    given(accountService.getAccountByUserId(anyLong()))
            .willReturn(accountDtos);
    //when
    //then
    mockMvc.perform(get("/account?user_id=1"))
            .andExpect(jsonPath("$[0].accountNumber").value("1234567890"))
            .andExpect(jsonPath("$[0].balance").value(1000L))
            .andExpect(jsonPath("$[1].accountNumber").value("1111111111"))
            .andExpect(jsonPath("$[1].balance").value(1000L))
            .andExpect(jsonPath("$[2].accountNumber").value("2222222222"))
            .andExpect(jsonPath("$[2].balance").value(1000L))
            .andDo(print());
}

 

Service Test

@Test
@DisplayName("계좌 요청하기")
public void getAccountSuccess() throws Exception {

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

    List<Account> accounts= Arrays.asList(
            Account.builder()
                    .accountUser(user)
                    .accountNumber("1234567890")
                    .balance(1000L)
                    .accountStatus(AccountStatus.IN_USE)
                    .build(),
            Account.builder()
                    .accountUser(user)
                    .accountNumber("1234567890")
                    .balance(2000L)
                    .accountStatus(AccountStatus.IN_USE)
                    .build(),
            Account.builder()
                    .accountUser(user)
                    .accountNumber("1234567890")
                    .balance(3000L)
                    .accountStatus(AccountStatus.IN_USE)
                    .build()
    );

    //given
    given(accountUserRepository.findById(anyLong()))
            .willReturn(Optional.of(user));

    given(accountRepository.findByAccountUser(any()))
            .willReturn(accounts);
    //when
    List<AccountDto> account = accountService.getAccountByUserId(1L);


    //then
    assertEquals(3,account.size());
    assertEquals(account.get(0).getAccountNumber(),"1234567890");
    assertEquals(account.get(0).getBalance(),1000L);
    assertEquals(account.get(1).getAccountNumber(),"1234567890");
    assertEquals(account.get(1).getBalance(),2000L);
    assertEquals(account.get(2).getAccountNumber(),"1234567890");
    assertEquals(account.get(2).getBalance(),3000L);
}

 

실패한 경우

@Test
public void failedGetAccounts() throws Exception {
    //given
    given(accountUserRepository.findById(anyLong()))
            .willReturn(Optional.empty());
    //when

    //then
    AccountException exception = assertThrows(AccountException.class, () -> accountService.getAccountByUserId(1L));

    assertEquals(exception.getErrorCode(), ErrorCode.USER_NOT_FOUND);
}

 

'Project' 카테고리의 다른 글

[Toss] 잔액사용(2)  (0) 2023.08.24
[Toss] 잔액 사용(1)  (0) 2023.08.23
[Toss] 계좌 확인(1)  (0) 2023.08.22
[Toss] 계좌 해지(2)  (0) 2023.08.22
[Toss] 계좌 해제(1)  (0) 2023.08.21