Skip to main content

Verifying

To verify that some calls occurred use verify:

verify {
foo.getAll()
}

If you want to verify suspending functions use verifySuspend:

verifySuspend {
foo.fetchAll()
}

Each verification is performed on unverified calls. In result, repeated verifications may give different results:

mock.getAt(1)

verify { mock.getAt(1) } // ✅
verify { mock.getAt(1) } // ❌ - no matching calls

Verification result depends on the VerifyMode. It determines the behavior and criteria for verification.

Global settings

You can change the default VerifyMode in the build.gradle file:

import dev.mokkery.verify.VerifyMode.exhaustiveOrder

mokkery {
defaultVerifyMode.set(exhaustiveOrder)
}

Soft modes family

By default, verify uses VerifyMode.soft. It checks only if calls from the verification block happened and marks all matching calls as verified.

mock.getAt(1)
mock.getAt(2)
mock.getAll()
verify {
mock.getAt(any())
// ✅ `getAt(1)` and `getAt(2)` are marked as verified.
}

You can restrict number of calls with atLeast, atMost, exactly and inRange:

mock.getAt(1)
mock.getAt(2)
mock.getAll()
verify(atMost(1)) {
mock.getAt(any())
// ❌ - 2 matching calls, but expected 1 at most
}

With any soft mode, each verification is performed independently so in example below, both verifications have the same result:

mock.getAll()
mock.getAll()

verify {
mock.getAll()
// ✅ - `getAll` was called at least once
}

verify {
mock.getAll()
mock.getAll()
// ✅ - `getAll` was called at least once
// the only difference is that `getAll` check is duplicated
}

In conclusion, there is no point of putting duplicated patterns in verify with soft mode.

Exhaustive

VerifyMode.exhaustive acts the same way as soft, but also checks if all calls have been verified.

warning

Exhaustiveness is only checked for mocks called within the verification block! To enforce stricter behavior, check strict exhaustiveness chapter.

mock.getAt(1)
mock.getAt(2)
mock.getAll()
verify(exhaustive) {
mock.getAt(any())
// ❌ - `getAll` not verified
}

Order

VerifyMode.order verifies that each call from the verification block happened once in the specified order:

mock.getAt(1)
mock.getAt(2)
mock.getAll()

verify(order) {
mock.getAt(any())
mock.getAll()
// ✅ - only `getAt(1)` and `getAll()` are marked as verified
}

mock.getAt(1)
mock.getAt(2)
mock.getAll()

verify(order) {
mock.getAll()
mock.getAt(any())
// ❌ - `getAt(any())` does not occur after `getAll()`
}

Exhaustive order

VerifyMode.exhaustiveOrder verifies that all calls occurred in the exact same way. No extra calls are allowed beyond what is specified for verification.

warning

Exhaustiveness is only checked for mocks called within the verification block! To enforce stricter behavior, check strict exhaustiveness chapter.

mock.getAt(1)
mock.getAt(2)
mock.getAll()
verify(exhaustiveOrder) {
mock.getAt(any())
mock.getAt(any())
mock.getAll()
// ✅ - each call matches
}

Check exhaustiveness manually

You can check if all calls are verified for given mock with verifyNoMoreCalls:

mock.getAt(1)
mock.getAll()
verify(soft) {
mock.getAt(1)
// ✅
}
verifyNoMoreCalls(mock) // ❌ - `getAll` was not verified!

Resetting registered calls

You can remove all registered calls with resetCalls:

mock.getAt(1)

resetCalls(mock)

verify(soft) {
mock.getAt(1)
// ❌ - registered calls were removed with `resetCalls`
}