Junit Test Application-39-계좌출금 기능 JUnit 테스트

jaegeunsong97·2023년 8월 9일
0

Junit Bank Application 깃허브

Junit Bank Application 기록 노션

  • 서비스 테스트
@Test
     public void 계좌출금_test() throws Exception {
          // given
          Long amount = 1000L;
          Long password = 1234L;
          Long userId = 1L;

          User ssar = newMockUser(1L, "ssar", "쌀");
          Account ssarAccount = newMockAccount(1L, 1111L, 1000L, ssar);

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

          ssarAccount.checkOwner(userId);

          ssarAccount.checkSamePassword(password);

          // ssarAccount.checkBalance(amount); // 생략한 이유 생각

          ssarAccount.withdraw(amount);

          // then
          assertThat(ssarAccount.getBalance()).isEqualTo(0L);
     }
  • 컨트롤러 테스트
@WithUserDetails(value = "ssar", setupBefore = TestExecutionEvent.TEST_EXECUTION)
     @Test
     public void withdrawAccount_test() throws Exception {
          // given
          AccountWithdrawRequestDto accountWithdrawRequestDto = new AccountWithdrawRequestDto();
          accountWithdrawRequestDto.setNumber(1111L);
          accountWithdrawRequestDto.setPassword(1234L);
          accountWithdrawRequestDto.setAmount(100L);
          accountWithdrawRequestDto.setGubun("WITHDRAW");

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

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

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

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

0개의 댓글