본문 바로가기
Java

@SoftAssertions를 사용하는 이유

by sangyunpark99 2024. 7. 17.
package com.example.payment.transferHistory;

import com.example.payment.transferHistory.entity.TransferHistory;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import java.math.BigDecimal;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

@DataJpaTest
public class TransferHistoryRepositoryTest {

    @Autowired
    TransferHistoryRepository transferHistoryRepository;

    @Test
    @DisplayName("계좌 이체 내역을 저장합니다.")
    void 계좌_이체_내역을_저장합니다() throws Exception{

        //given
        TransferHistory transferHistory = TransferHistory.builder()
                .withdrawalAccountNumber("0123456789")
                .depositAccountNumber("1234567890")
                .transferAmount(BigDecimal.valueOf(10000))
                .amountAfterWithdrawal(BigDecimal.valueOf(0))
                .amountAfterDeposit(BigDecimal.valueOf(20000))
                .build();

        //when
        TransferHistory savedTransferHistory = transferHistoryRepository.save(transferHistory);

        //then
        SoftAssertions.assertSoftly(softAssertions -> {
            softAssertions.assertThat(savedTransferHistory.getWithdrawalAccountNumber()).isEqualTo(transferHistory.getWithdrawalAccountNumber());
            softAssertions.assertThat(savedTransferHistory.getDepositAccountNumber()).isEqualTo(transferHistory.getDepositAccountNumber());
            softAssertions.assertThat(savedTransferHistory.getTransferAmount()).isEqualTo(transferHistory.getTransferAmount());
            softAssertions.assertThat(savedTransferHistory.getAmountAfterDeposit()).isEqualTo(transferHistory.getAmountAfterDeposit());
            softAssertions.assertThat(savedTransferHistory.getAmountAfterWithdrawal()).isEqualTo(transferHistory.getAmountAfterWithdrawal());
        });
    }
}

 

Q. SoftAssertions는 왜 사용하는가?

각 Assertions들이 서로 독립적으로 영향을 받지 않아, 이전 Assertions가 오류가 발생해도 다음 Assertions도 테스트 결과를 받아볼 수 있다. 한번에 Assertions 테스트에 대한 피드백을 받을 수 있다.

 

🧪 [실험] SoftAssertions가 정말 독립적으로 Assertions를 실행할까? (아래 링크로 확인)

https://sangyunpark99.tistory.com/10