도메인 오브젝트 테스트
2024. 7. 8. 14:26ㆍ토비의 스프링 6
도메인 모델 아키텍처 패턴
도메인 로직, 비즈니스 로직을 어디에 둘 지를 결정하는 패턴
- 트랜잭션 스크립트 - 서비스 메소드 (PaymentService.prepare)
- 도메인 모델 - 도메인 모델 오브젝트(Payment)
Payment
package tobyspring.hellospring.payment;
import java.math.BigDecimal;
import java.time.LocalDateTime;
public class Payment {
private Long orderId;
private String currency;
private BigDecimal foreignCurrenyAmount; //돈과같은 정확한 소수점 계산을 위한 자료형
private BigDecimal exRate;
private BigDecimal convertedAmount;
private LocalDateTime validUntil;
public Payment(Long orderId, String currency, BigDecimal foreignCurrenyAmount, BigDecimal exRate, BigDecimal convertedAmount, LocalDateTime validUntil) {
this.orderId = orderId;
this.currency = currency;
this.foreignCurrenyAmount = foreignCurrenyAmount;
this.exRate = exRate;
this.convertedAmount = convertedAmount;
this.validUntil = validUntil;
}
public static Payment createPrepared(Long orderId, String currency, BigDecimal foreignCurrenyAmount, BigDecimal exRate,LocalDateTime now){
BigDecimal convertedAmount = foreignCurrenyAmount.multiply(exRate); // * 사용안하고 multiply사용
LocalDateTime validUntil = now.plusMinutes(30);
return new Payment(orderId,currency,foreignCurrenyAmount,exRate,convertedAmount,validUntil);
}
public Long getOrderId() {
return orderId;
}
public String getCurrency() {
return currency;
}
public BigDecimal getForeignCurrenyAmount() {
return foreignCurrenyAmount;
}
public BigDecimal getExRate() {
return exRate;
}
public BigDecimal getConvertedAmount() {
return convertedAmount;
}
public LocalDateTime getValidUntil() {
return validUntil;
}
@Override
public String toString() {
return "Payment{" +
"orderId=" + orderId +
", currency='" + currency + '\'' +
", foreignCurrenyAmount=" + foreignCurrenyAmount +
", exRate=" + exRate +
", convertedAmount=" + convertedAmount +
", validUntil=" + validUntil +
'}';
}
}
PaymentTest
package tobyspring.hellospring.payment;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import java.time.Clock;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class PaymentTest {
@Test
public void createPrepared() throws Exception{
Clock clock = Clock.fixed(Instant.now(), ZoneId.systemDefault());
//given
Payment payment = Payment.createPrepared(1L,"USD", BigDecimal.TEN,BigDecimal.valueOf(1_000), LocalDateTime.now(clock));
//when
//then
Assertions.assertThat(payment.getConvertedAmount()).isEqualByComparingTo(BigDecimal.valueOf(10_000));
Assertions.assertThat(payment.getValidUntil()).isEqualTo(LocalDateTime.now(clock).plusMinutes(30));
}
}
@Test
void isValid(){
Clock clock = Clock.fixed(Instant.now(),ZoneId.systemDefault());
Payment payment = Payment.createPrepared(1L,"USD", BigDecimal.TEN,BigDecimal.valueOf(1_000), LocalDateTime.now(clock));
Assertions.assertThat(payment.isValid(clock)).isTrue();
Assertions.assertThat(payment.isValid(Clock.offset(clock, Duration.of(30, ChronoUnit.MINUTES)))).isFalse();
}
'토비의 스프링 6' 카테고리의 다른 글
| 변하지 않는 코드 분리하기 (0) | 2024.07.10 |
|---|---|
| 변하는 코드 분리하기 - 메소드 추출 (0) | 2024.07.10 |
| 학습 테스트(Learning Test) (0) | 2024.07.08 |
| 테스트와 DI (0) | 2024.07.08 |
| 테스트 (0) | 2024.07.06 |