Skip to content

Instantly share code, notes, and snippets.

@robfletcher
Created March 16, 2019 16:44
Show Gist options
  • Save robfletcher/7c67b728e1c657f1b03ca6303b0f67af to your computer and use it in GitHub Desktop.
Save robfletcher/7c67b728e1c657f1b03ca6303b0f67af to your computer and use it in GitHub Desktop.
Demonstration of error verifying calls to a lambda that returns `Unit`
import io.mockk.mockk
import io.mockk.verify
import org.junit.jupiter.api.Test
class MockkUnitLambdaTest {
@Test
fun mockUnitLambda() {
val fn: (String) -> Unit = mockk(relaxUnitFun = true)
fn("foo")
verify { fn("foo") }
}
@Test
fun mockUnitLambdaInvoke() {
val fn: (String) -> Unit = mockk(relaxUnitFun = true)
fn.invoke("foo")
verify { fn.invoke("foo") }
}
@Test
fun mockUnitLambdaRelaxed() {
val fn: (String) -> Unit = mockk(relaxed = true)
fn("foo")
verify { fn("foo") }
}
}
@robfletcher
Copy link
Author

The first 2 tests fail with io.mockk.MockKException: no answer found for: Function1(#1).invoke(foo). The final test passes because it uses relaxed = true.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment