void
methods
Mockito’s when
method doesn’t work with void
methods. To create a stub that doesn’t return anything, the doNothing
method is used.
val mockedFile = mock(File::class.java)
doNothing().`when`(mockedFile).write(any())
MockK doesn’t have any restrictions with these methods, as they return Unit in Kotlin. As a result, the standard returns
infix function can be used.
val mockedFile = mockk<File>()
every { mockedFile.write(any()) } returns Unit
MockK also provides the justRun
method as a shorthand for every { x } returns Unit
. For more information, see the Returning Unit tip.
val mockedFile = mockk<File>()
justRun { mockedFile.write(any()) }
当前内容版权归 Tiger Oakes 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 Tiger Oakes .