๊ฐ์
captured ๋ฅผ ์ฌ์ฉํด์ ์ฃผ์ด์ง stub ์ ์กฐ๊ฑด์ ๋ก ์ฒ๋ฆฌํ ์ ์๋ค.
๋น์ฆ๋์ค ์ฝ๋
์๋์ ๊ฐ์ ๋ก์ง์ด ์๋ค๊ณ ๊ฐ์ ํ๋ค.
(coffeeGetService -> coffeeRepository)
// ์ปคํผ ๊ฐ์ฒด
data class Coffee(
val name: String,
val price: Long
)
// ์ปคํผ ์กฐํ ์๋น์ค
class CoffeeGetService(
private val coffeeRepository: CoffeeRepository
) {
// ๊ฐ๊ฒฉ์ ๋ฐ๋ฅธ ์ปคํผ๋ชฉ๋ก์ ์กฐํํ๋ค.
fun getCoffeesByPrices(prices: List<Long>): List<Coffee> {
return prices.map {
coffeeRepository.findAllByPrice(it)
}
.flatten()
.distinctBy { it }
}
}
// ์ปคํผ ๋ ํ์งํ ๋ฆฌ : ๋ท๋จ์ ์คํ ๋ฆฌ์ง์ ์ฐ๊ฒฐ๋์๋ค๊ณ ๊ฐ์ .
class CoffeeRepository {
fun findAllByPrice(price: Long): List<Coffee> {
return emptyList()
}
}
ํ ์คํธ์ฝ๋
coffeeRepository.findAllByPrice(price: Long) ์ ์ ๋ฌ๋ฐ์ ๋, capture(price) ๋ก ๊ฑด๋ค์ค๋ค. ์ด๋ ๊ฒ ํ๋ฉด ์ฐ๋ฆฌ๊ฐ mocking ๋ ๊ฐ์ฒด์ ํ ์คํธ๋ฅผ ์ํ ์ธ์๋ฅผ ๊ฑด๋ค์ค ๋, ์กฐ๊ฑด์ ๋ง๊ฒ ์๋ต๊ฐ์ ์ฒ๋ฆฌํ ์ ์๋ค. (answers {} ๋ธ๋ญ์ด ํด๋น ์์)
class CapturingTest {
private val coffeeRepository: CoffeeRepository = mockk()
private val coffeeGetService = CoffeeGetService(coffeeRepository)
@Test
@DisplayName("๊ธ์ก์ ๊ฑด๋ค์ฃผ๊ณ , ๊ธ์ก์ ๋ง๋ ์ปคํผ์ข
๋ฅ๋ฅผ ๋ฐํํ๋ค.")
fun capturingTest() {
// given
val price = slot<Long>()
every { coffeeRepository.findAllByPrice(capture(price)) } answers {
when (price.captured) {
3000L -> {
listOf(Coffee("์๋ฉ๋ฆฌ์นด๋
ธ", 3000L))
}
5000L -> {
listOf(Coffee("์นดํ๋ผ๋ผ", 5000L))
}
8000L -> {
listOf(Coffee("์ฝ๋๋ธ๋ฃจ", 8000L))
}
else -> {
emptyList()
}
}
}
// when
val actual = coffeeGetService.getCoffeesByPrices(listOf(3000L, 5000L, 9000L))
// then
actual.asClue { coffee ->
coffee.size shouldBe 2
coffee.minByOrNull { it.price }!!.name shouldBe "์๋ฉ๋ฆฌ์นด๋
ธ"
coffee.maxByOrNull { it.price }!!.name shouldBe "์นดํ๋ผ๋ผ"
}
}
}
ํ ์คํธ ์ ํ๊ณ ์ ํ๋ ๊ฐ์ฒด์ ๊ฑฐ๊ธฐ์ ์์กด๋ mock ๊ฐ์ฒด์ ์๋ต์ ์กฐ๊ฑด์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ ์ฃผ๊ณ ์ถ์ ๋ ์ฌ์ฉํ๋ฉด ์ข๊ฒ ๋ค.
reference
'open source' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
2022-11-01 [mysql] : Locking Reads ์ฝ๊ธฐ (S-LOCK & X-LOCK) (0) | 2022.11.01 |
---|---|
2022-08-20 [redis] : redis-cli ์ hex ๋ฅผ ์์ string ์ผ๋ก ๋ณด๊ณ ์ ํ ๋. (0) | 2022.08.20 |
2022-05-15 [mockk] wasNot called ์ฌ์ฉ (0) | 2022.05.15 |
2021-12-30 [jmeter] jmeter ์ฑ๋ฅ ํ ์คํธ ์ํ (2) (0) | 2021.12.30 |
2021-12-19 [jmeter] jmeter ์ค์น ๋ฐ ์คํ (1) (0) | 2021.12.19 |