mockk

inline fun <T : Any> mockk(name: String? = null, relaxed: Boolean = false, vararg moreInterfaces: KClass<*>, relaxUnitFun: Boolean = false, mockValidator: MockkValidator = MockkValidator(RestrictMockkConfiguration()), block: T.() -> Unit = {}): T

Builds a new mock for specified class.

A mock is a fake version of a class that replaces all the methods with fake implementations.

By default, every method that you wish to mock should be stubbed using every. Otherwise, it will throw when called, so you know if you forgot to mock a method. If relaxed or relaxUnitFun is set to true, methods will automatically be stubbed.

Parameters

name

mock name

relaxed

allows creation with no specific behaviour. Unstubbed methods will not throw.

moreInterfaces

additional interfaces for this mockk to implement, in addition to the specified class.

relaxUnitFun

allows creation with no specific behaviour for Unit function. Unstubbed methods that return Unit will not throw, while other methods will still throw unless they are stubbed.

block

block to execute after mock is created with mock as a receiver. Similar to using apply on the mock object.

Samples