프로그래밍 노트/TEST를 해보자
[Kotest] 몇 가지 팁
깡냉쓰
2023. 10. 26. 10:00
728x90
반응형
withData는 중첩 테스트기 때문에 outer test에서 사용해야 한다.
FreeSpec
// error
"test" {
withData(1,2,3) {
//..
}
}
// ok
"test" - {
withData(1,2,3) {
//..
}
}
ShouldSpec
context("test") {
withData(1,2,3) {
// ..
}
}
Spyk - recordPrivateCalls 속성을 이용하면 private 메소드를 손 쉽게 mocking할 수 있다.
- kotest private 메소드 mocking
val target = spyk(Service(), recordPrivateCalls = true)
"test" {
every { target["validate"](obj) } returns Unit
}
- validate는 private method 임
static mehod는 mockkStatic을 사용하여 mocking할 수 있다.
- kotest static 메소드 mocking
mockkStatic(LocalDate::class)
val nowFixedDate: LocalDate = LocalDate.of(2023, 6, 26)
every { LocalDate.now() } returns nowFixedDate
mocking된 객체의 이력(verify, mock method)를 삭제하기 위해서는 clearMocks를 사용한다.
val neighborIoService: NeighborIoService = mockk()
clearMocks(neighborIoService)
- 테스트 메서드간 verify 이력 공유는 좋지 않다.
mockObject를 이용하면 싱글톤 객체 또는 Kotlin object
로 정의된 클래스를 mocking할 수 있다.
- kotest singleton 객체 mocking, enum mocking
mockkObject(CryptoOriented.INSIDE_YEARLY)
every { CryptoOriented.INSIDE_YEARLY.decrypt(any()) } returns Secret(
cipher = "cipher", oriented = CryptoOriented.INSIDE_YEARLY)
728x90
반응형