RelaxedMockk
kotlin mockk test
RelaxedMockk
A relaxed mock is the mock that returns some simple value for all functions. This allows you to skip specifying behavior for each case, while still stubbing things you need. For reference types, chained mocks are returned.
val car = mockk<Car>(relaxed = true)
car.drive(Direction.NORTH) // returns null
verify { car.drive(Direction.NORTH) }
confirmVerified(car)
A typical mocked object will throw MockKException if we try to call a method where the return value hasn’t been specified. If we don’t want to describe the behavior of each method, then we can use a relaxed mock
@Test
fun test() {
// given
val service = mockk<AuthService>(relaxed = true)
// when
val result = service.signIn("Any Param")
// then
assertEquals("", result)
}
We don't need Stubbing. We could've also used the @RelaxedMockk annotation:
class RelaxedMockKUnitTest
@RelaxedMockK
lateinit var service: TestableService
// Tests here
}