MokkerySuiteScope

A scope for a test suite that uses Mokkery mocks. It enables automation and strict exhaustiveness checks.

Every mock created with MokkerySuiteScope.mock, MokkerySuiteScope.mockMany, or MokkerySuiteScope.spy becomes a part of given scope.

Mokkery provides MokkerySuiteScope.verify and MokkerySuiteScope.verifySuspend. When used with an exhaustive dev.mokkery.verify.VerifyMode, all mocks in this scope (not just those inside the verify block) are checked for exhaustiveness.

Additionally, you can call MokkerySuiteScope.verifyNoMoreCalls to ensure that all mocks in the scope have had their calls fully verified.

Regular test classes

For regular test classes, mark them with MokkerySuiteScope interface. MokkerySuiteScope has a default mokkeryContext implementation that throws an exception. This property is meant to be automatically overridden by the compiler plugin when not explicitly set by the user. Manual implementation is not recommended.

The compiler plugin overrides this property only in classes that directly inherit from MokkerySuiteScope. If MokkerySuiteScope is inherited indirectly, it is not implemented.

class ClassTest : MokkerySuiteScope {
private val a = mock<A>()
private val b = mock<B>()

@Test
fun test() {
a.call(1)
b.call(2)
// Verification fails - b.call(2) was not verified
verify(exhaustive) {
a.call(1)
}
}
}

Other cases

If you cannot mark your test class with MokkerySuiteScope, create an instance using the MokkerySuiteScope function.

with(MokkerySuiteScope()) {
val a = mock<A>()
val b = mock<B>()
a.call(1)
b.call(2)
// Verification fails
// Unverified call: b.call(2)
verify(exhaustive) {
a.call(1)
}
}

Properties

Link copied to clipboard

Returns all mocks from this MokkerySuiteScope.

Link copied to clipboard
open override val mokkeryContext: MokkeryContext

Functions

Link copied to clipboard
inline fun <T : Any> MokkerySuiteScope.mock(mode: MockMode = MokkeryCompilerDefaults.mockMode, block: T.() -> Unit = { }): T

Provides mock implementation of given type T. It is a child of given MokkerySuiteScope.

Link copied to clipboard
inline fun <T1 : Any, T2 : Any> MokkerySuiteScope.mockMany(mode: MockMode = MokkeryCompilerDefaults.mockMode, block: MockMany2<T1, T2>.() -> Unit = { }): MockMany2<T1, T2>

Provides mock implementation of T1 and T2. It is a child of given MokkerySuiteScope.

inline fun <T1 : Any, T2 : Any, T3 : Any> MokkerySuiteScope.mockMany(mode: MockMode = MokkeryCompilerDefaults.mockMode, block: MockMany3<T1, T2, T3>.() -> Unit = { }): MockMany3<T1, T2, T3>

Provides mock implementation of T1, T2 and T3. It is a child of given MokkerySuiteScope.

inline fun <T1 : Any, T2 : Any, T3 : Any, T4 : Any> MokkerySuiteScope.mockMany(mode: MockMode = MokkeryCompilerDefaults.mockMode, block: MockMany4<T1, T2, T3, T4>.() -> Unit = { }): MockMany4<T1, T2, T3, T4>

Provides mock implementation of T1, T2, T3 and T4. It is a child of given MokkerySuiteScope.

inline fun <T1 : Any, T2 : Any, T3 : Any, T4 : Any, T5 : Any> MokkerySuiteScope.mockMany(mode: MockMode = MokkeryCompilerDefaults.mockMode, block: MockMany5<T1, T2, T3, T4, T5>.() -> Unit = { }): MockMany5<T1, T2, T3, T4, T5>

Provides mock implementation of T1, T2, T3, T4 and T5. It is a child of given MokkerySuiteScope.

Link copied to clipboard
inline fun <T : Any> MokkerySuiteScope.spy(obj: T, block: T.() -> Unit = { }): T

Returns given obj wrapped with a spying implementation of T. It is a child of given MokkerySuiteScope.

Link copied to clipboard
fun MokkerySuiteScope.verify(mode: VerifyMode = MokkeryCompilerDefaults.verifyMode, block: ArgMatchersScope.() -> Unit)

Asserts that calls sequence defined in block satisfies given mode.

Link copied to clipboard

Asserts that all mocks from given MokkerySuiteScope have no unverified calls.

Link copied to clipboard
fun MokkerySuiteScope.verifySuspend(mode: VerifyMode = MokkeryCompilerDefaults.verifyMode, block: suspend ArgMatchersScope.() -> Unit)

Just like verify, but allows suspendable function calls.