Mocking start with one call, the
mockk function. This function takes in a class and returns a fake version of it, where all functions are present but will throw when called.
import io.mockk.mockk val mockedFile = mockk<File>()
- Stub out behaviour
returnsto define behaviour.
- Verify that functions were called
verifyto check that a function was used.
- Automatically stub by relaxing
- How to change the default
- Spy on existing classes
spykto mix mocks and real classes.
- Coroutines and suspend functions
coVerify, and more to mock coroutines.
- Mock constructors in code you don't own
- Advanced mocking with
- Mock singleton objects and static methods
- Advanced static mocking with
- Mock top-level and extension functions
- Mocking top-level functions with
- Clear state
- (TODO) Clearing the state of a mock.
- Create many mocks quickly with annotations
- Chain mocks into hierarchies
- (TODO) Building a mock with less code using lambdas.
- Create more complicated answers for stubs
returnsjust isn’t enough.