본문 바로가기
Java & Kotlin/Kotlin

[MockK] 인자 값 그대로 리턴하기

by devson 2022. 4. 14.

Mock 객체를 사용하다보면 JpaRepository.saveAll과 같이 입력값을 그대로 리턴해주고 싶은 경우가 생길 수 있다.

이 경우 returnsArgument를 사용하면 된다.

 

interface TestTarget {
fun function(arg1: Any, arg2: Any): Any
}
internal class MyRepositoryMockTest : DescribeSpec({
it(" returnsArgument 0은 첫번째 인자를 리턴한다") {
// given
val testTargetMock= mockk<TestTarget>()
// when
every { testTargetMock.function(any(), any()) } returnsArgument 0
// then
testTargetMock.function("Hello", "World".toList()) shouldBe "Hello"
}
it(" returnsArgument 1은 두번째 인자를 리턴한다") {
// given
val testTargetMock= mockk<TestTarget>()
// when
every { testTargetMock.function(any(), any()) } returnsArgument 1
// then
testTargetMock.function("Hello", "World".toList()) shouldBe listOf('W', 'o', 'r', 'l', 'd')
}
})

 

참고