Comprehensive mocking library for Kotlin with native coroutine support and advanced DSL features.
—
Core mock and spy creation functions with comprehensive configuration options for creating different types of test doubles in Kotlin testing scenarios.
Creates a mock instance that replaces all methods with fake implementations.
/**
* Creates a mock for the specified class type
* @param name Optional name for the mock (useful for debugging)
* @param relaxed If true, unstubbed methods return default values instead of throwing
* @param moreInterfaces Additional interfaces for the mock to implement
* @param relaxUnitFun If true, only Unit-returning methods are relaxed
* @param mockValidator Validator for mockable classes (default: MockkValidator)
* @param block Initialization block executed after mock creation
* @return Mock instance of type T
*/
inline fun <reified T : Any> mockk(
name: String? = null,
relaxed: Boolean = false,
vararg moreInterfaces: KClass<*>,
relaxUnitFun: Boolean = false,
mockValidator: MockkValidator = MockkValidator(RestrictMockkConfiguration()),
block: T.() -> Unit = {}
): TUsage Examples:
// Basic mock
val userService = mockk<UserService>()
// Named mock for debugging
val namedMock = mockk<UserService>(name = "TestUserService")
// Relaxed mock - unstubbed methods return defaults
val relaxedMock = mockk<UserService>(relaxed = true)
// Mock with additional interfaces
val multiInterfaceMock = mockk<UserService>(
moreInterfaces = arrayOf(Serializable::class, Cloneable::class)
)
// Mock with initialization block
val initializedMock = mockk<UserService> {
// Setup code here
}Creates a spy that enables a mix of mocked behavior and real behavior.
/**
* Creates a spy using the default constructor
* @param name Optional name for the spy
* @param moreInterfaces Additional interfaces for the spy to implement
* @param recordPrivateCalls If true, enables verification of private method calls
* @param block Initialization block executed after spy creation
* @return Spy instance of type T
*/
inline fun <reified T : Any> spyk(
name: String? = null,
vararg moreInterfaces: KClass<*>,
recordPrivateCalls: Boolean = false,
block: T.() -> Unit = {}
): T
/**
* Creates a spy by copying from an existing object
* @param objToCopy Existing object to copy field values from
* @param name Optional name for the spy
* @param moreInterfaces Additional interfaces for the spy to implement
* @param recordPrivateCalls If true, enables verification of private method calls
* @param block Initialization block executed after spy creation
* @return Spy instance of type T
*/
inline fun <reified T : Any> spyk(
objToCopy: T,
name: String? = null,
vararg moreInterfaces: KClass<*>,
recordPrivateCalls: Boolean = false,
block: T.() -> Unit = {}
): TUsage Examples:
// Spy with default constructor
val userSpy = spyk<UserService>()
// Spy from existing object
val existingUser = UserService("config")
val userSpyFromObject = spyk(existingUser)
// Spy with private call recording
val recordingSpyService = spyk<UserService>(recordPrivateCalls = true)
// Spy with initialization
val initializedSpy = spyk<UserService> {
// Partial stubbing - some methods mocked, others call original
}Creates a mock using explicit KClass instead of reified generics.
/**
* Creates a mock for an arbitrary class using KClass
* @param type The KClass of the type to mock
* @param name Optional name for the mock
* @param relaxed If true, unstubbed methods return default values
* @param moreInterfaces Additional interfaces for the mock to implement
* @param relaxUnitFun If true, only Unit-returning methods are relaxed
* @param block Initialization block executed after mock creation
* @return Mock instance of type T
*/
inline fun <T : Any> mockkClass(
type: KClass<T>,
name: String? = null,
relaxed: Boolean = false,
vararg moreInterfaces: KClass<*>,
relaxUnitFun: Boolean = false,
block: T.() -> Unit = {}
): TUsage Examples:
// Mock using KClass when type inference isn't available
val serviceClass: KClass<UserService> = UserService::class
val service = mockkClass(serviceClass)
// In generic contexts
fun <T : Any> createMockOfType(clazz: KClass<T>): T {
return mockkClass(clazz, relaxed = true)
}Utility function to check if an object is a MockK mock.
/**
* Checks if the provided object is a MockK mock of a certain type
* @param mock Object to check
* @param regular Check for regular mock (default: true)
* @param spy Check for spy (default: false)
* @param objectMock Check for object mock (default: false)
* @param staticMock Check for static mock (default: false)
* @param constructorMock Check for constructor mock (default: false)
* @return true if the object matches the specified mock types
*/
fun isMockKMock(
mock: Any,
regular: Boolean = true,
spy: Boolean = false,
objectMock: Boolean = false,
staticMock: Boolean = false,
constructorMock: Boolean = false
): BooleanUsage Examples:
val mock = mockk<UserService>()
val spy = spyk<UserService>()
// Check if object is any kind of MockK mock
val isMock = isMockKMock(mock) // true
// Check specific mock types
val isSpy = isMockKMock(spy, regular = false, spy = true) // true
val isRegularMock = isMockKMock(mock, regular = true) // trueRelaxed mocks return default values for unstubbed methods instead of throwing exceptions.
// Fully relaxed - all unstubbed methods return defaults
val relaxedMock = mockk<UserService>(relaxed = true)
// Unit function relaxed - only Unit-returning methods are relaxed
val unitRelaxedMock = mockk<UserService>(relaxUnitFun = true)Mocks can implement additional interfaces beyond their primary type.
interface Auditable {
fun audit(): String
}
val auditableMock = mockk<UserService>(
moreInterfaces = arrayOf(Auditable::class)
)
// Mock now implements both UserService and Auditable
every { (auditableMock as Auditable).audit() } returns "audited"Named mocks are useful for debugging and identifying mocks in test output.
val namedMock = mockk<UserService>(name = "TestUserService")
// Mock will be identified as "TestUserService" in error messagesInstall with Tessl CLI
npx tessl i tessl/maven-io-mockk--mockk