Mockk
Mockk 를 활용한 테스트
kotlin mockk test
@Mockk
Mockk which is similar to Mockito but better suited for Kotlin.
@ExtendWith(MockKExtension::class)
internal class UserServiceTest: DescribeSpec() {
companion object {
private const val DUPLICATED_EMAIL = "abc@naver.com"
}
@MockK private lateinit var userStore: UserStore
@MockK private lateinit var userReader: UserReader
@InjectMockKs private lateinit var userService: UserService
init {
describe("회원 가입") {
val validUser = UserCommand.RegisterUser(email = DUPLICATED_EMAIL, password = "123!abACC123")
it("이메일이 중복되는 경우 회원 가입 실패") {
assertThrows<DuplicatedEmailException> { userService.register(validUser) }
}
}
}
override fun beforeEach(testCase: TestCase) {
// Unit-returning functions to be relaxed
MockKAnnotations.init(this, relaxUnitFun = true)
every { userReader.existsByEmail(DUPLICATED_EMAIL) } returns true
every { userStore.register(any()) } just Runs
}
}
- MockKAnnotations: Initializes properties annotated with @MockK, @RelaxedMockK, @Slot and @SpyK in provided object.
- @Mockk: If you want use to this annotation then follow guides
-
@MockK @AdditionalInterface(Runnable::class) private lateinit var car: Car
- Dependency Injection:
lateinit var
-