Create a mock
The syntax to create a mock is very similar in Mockito and MockK. However, the behaviour is slightly different.
// Mockito
val mockedFile = mock(File::class.java)
mockedFile.read() // does nothing
// MockK
val mockedFile = mockk<File>()
mockedFile.read() // throws because the method was not stubbed
Mocked objects in MockK resemble Mockito’s STRICT_STUBS mode by default. If a method is not stubbed, then it will throw. This makes it easier to catch methods that are being called when you do not expect it, or when methods are being called with different arguments.
Mockito’s default lenient behaviour can be replicated with the relaxed
setting. Relaxed mocks will have default stubs for all methods.
// MockK
val mockedFile = mockk<File>(relaxed = true)
mockedFile.read() // will not throw
If desired, you can choose to only relax methods that return Unit
.
// MockK
val mockedFile = mockk<File>(relaxUnitFun = true)
mockedFile.read() // returns Unit, will not throw
mockedFile.exists() // throws as the method returns Boolean
当前内容版权归 Tiger Oakes 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 Tiger Oakes .