Backend
home
🕖

Clock을 이용한 시간 테스트

생성일
2025/01/24 05:52
태그
package tobyspring.hellospring.payment; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit.jupiter.SpringExtension; import java.io.IOException; import java.math.BigDecimal; import java.time.Clock; import java.time.LocalDateTime; import static java.math.BigDecimal.valueOf; import static org.assertj.core.api.Assertions.assertThat; @ExtendWith(SpringExtension.class) @ContextConfiguration(classes = TestPaymentConfig.class) class PaymentServiceSpringTest { // 의존 관계 주입 @Autowired PaymentService paymentService; @Autowired Clock clock; @Autowired ExRateProviderStub exRateProviderStub; @Test void convertedAmount() throws IOException { // exRate: 1000 Payment payment = paymentService.prepare(1L, "USD", BigDecimal.TEN); assertThat(payment.getExRate()).isEqualByComparingTo(valueOf(1_000)); assertThat(payment.getConvertedAmount()).isEqualByComparingTo(valueOf(10_000)); // exRate: 500 exRateProviderStub.setExRate(valueOf(500)); Payment payment2 = paymentService.prepare(1L, "USD", BigDecimal.TEN); assertThat(payment2.getExRate()).isEqualByComparingTo(valueOf(500)); assertThat(payment2.getConvertedAmount()).isEqualByComparingTo(valueOf(5_000)); } @Test void validUntil() throws IOException { // 실행 Payment payment = paymentService.prepare(1L, "USD", BigDecimal.TEN); // valid until이 prepare() 30분 뒤로 설정됐는가? LocalDateTime now = LocalDateTime.now(this.clock); LocalDateTime expectedValidUntil = now.plusMinutes(30); Assertions.assertThat(payment.getValidUntil()).isEqualTo(expectedValidUntil); } }
Java
복사