1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
| class ChristmasDiscountWithoutClockTest {
private val sut = ChristmasDiscountWithoutClock()
@Test
fun christmas() {
val christmas: LocalDate = LocalDate.of(2015, Month.DECEMBER, 25)
val finalValue =
resetClock(christmas) {
sut.applyDiscount(100.0)
}
assertThat(finalValue).isCloseTo(85.0, offset(0.001))
}
@Test
fun notChristmas() {
val notChristmas: LocalDate = LocalDate.of(2015, Month.DECEMBER, 26)
val finalValue =
resetClock(notChristmas) {
sut.applyDiscount(100.0)
}
assertThat(finalValue).isCloseTo(100.0, offset(0.001))
}
}
fun <T> resetClock(
targetLocalDate: LocalDate,
block: () -> T,
): T {
mockkStatic(LocalDate::class)
every { LocalDate.now() } returns targetLocalDate
return block.invoke().also { unmockkStatic(LocalDate::class) }
}
|