Method index #
Index of MockK methods and corresponding guidebook chapters.
Top level functions #
mockk<T>(...)
#
Chapter:
Mocking
builds a regular mock
spyk<T>()
#
Chapter:
Spy on existing classes
builds a spy using the default constructor
spyk(obj)
#
Chapter:
Spy on existing classes
builds a spy by copying from obj
slot
#
Chapter:
Capture arguments to check later
creates a capturing slot
every
#
Chapter:
Stub out behaviour
starts a stubbing block
coEvery
#
Chapter:
Coroutines and suspend functions
starts a stubbing block for coroutines
verify
#
Chapter:
Verify that functions were called
starts a verification block
coVerify
#
Chapter:
Coroutines and suspend functions
starts a verification block for coroutines
verifyAll
#
Chapter:
Verify that functions were called
starts a verification block that should include all calls
coVerifyAll
#
Chapter:
Coroutines and suspend functions
starts a verification block that should include all calls for coroutines
verifyOrder
#
Chapter:
Verify that functions were called
starts a verification block that checks the order
coVerifyOrder
#
Chapter:
Coroutines and suspend functions
starts a verification block that checks the order for coroutines
verifySequence
#
Chapter:
Verify that functions were called
starts a verification block that checks whether all calls were made in a specified sequence
coVerifySequence
#
Chapter:
Coroutines and suspend functions
starts a verification block that checks whether all calls were made in a specified sequence for coroutines
excludeRecords
#
Chapter:
Verify that functions were called
exclude some calls from being recorded
confirmVerified
#
Chapter:
Verify that functions were called
confirms that all recorded calls were verified
clearMocks
#
Chapter:
Clear state
clears specified mocks
registerInstanceFactory
#
Chapter: TODO
allows you to redefine the way of instantiation for certain object
mockkClass
#
Chapter: TODO
builds a regular mock by passing the class as parameter
mockkObject
#
Chapter:
Mock singleton objects and static methods
makes an object an object mock or clears it if was already transformed
unmockkObject
#
Chapter:
Mock singleton objects and static methods
makes an object mock back to a regular object
mockkStatic
#
Chapter:
Mock singleton objects and static methods
makes a static mock out of a class or clears it if it was already transformed
unmockkStatic
#
Chapter:
Mock singleton objects and static methods
makes a static mock back to a regular class
clearStaticMockk
#
Chapter: TODO
clears a static mock
mockkConstructor
#
Chapter:
Mock constructors in code you don’t own
makes a constructor mock out of a class or clears it if it was already transformed
unmockkConstructor
#
Chapter:
Mock constructors in code you don’t own
makes a constructor mock back to a regular class
clearConstructorMockk
#
Chapter: TODO
clears the constructor mock
unmockkAll
#
Chapter: TODO
unmocks object, static and constructor mocks
clearAllMocks
#
Chapter: TODO
clears regular, object, static and constructor mocks
Matchers #
By default, simple arguments are matched using eq()
.
any()
#
Chapter:
Allow any argument
matches any argument
allAny()
#
Chapter:
Allow any argument
special matcher that uses any()
instead of eq()
for matchers that are provided as simple arguments
isNull()
#
Chapter: TODO
checks if the value is null
isNull(inverse=true)
#
Chapter: TODO
checks if the value is not null
ofType(type)
#
Chapter:
Argument of a certain type
checks if the value belongs to the type
match { it.startsWith("string") }
#
Chapter: TODO
matches via the passed predicate
coMatch { it.startsWith("string") }
#
Chapter: TODO
matches via the passed coroutine predicate
matchNullable { it?.startsWith("string") }
#
Chapter: TODO
matches nullable value via the passed predicate
coMatchNullable { it?.startsWith("string") }
#
Chapter: TODO
matches nullable value via the passed coroutine predicate
eq(value)
#
Chapter:
Check equality
matches if the value is equal to the provided value via the deepEquals
function
eq(value, inverse=true))
#
Chapter:
Check equality
matches if the value is not equal to the provided value via the deepEquals
function
neq(value)
#
Chapter:
Check equality
matches if the value is not equal to the provided value via deepEquals
function
refEq(value)
#
Chapter:
Check equality
matches if the value is equal to the provided value via reference comparison
refEq(value, inverse=true)
#
Chapter:
Check equality
matches if the value is not equal to the provided value via reference comparison
nrefEq(value)
#
Chapter:
Check equality
matches if the value is not equal to the provided value via reference comparison
cmpEq(value)
#
Chapter:
Check equality
matches if the value is equal to the provided value via the compareTo
function
less(value)
#
Chapter:
Comparables
matches if the value is less than the provided value via the compareTo
function
more(value)
#
Chapter:
Comparables
matches if the value is more than the provided value via the compareTo
function
less(value, andEquals=true)
#
Chapter:
Comparables
matches if the value is less than or equal to the provided value via the compareTo
function
more(value, andEquals=true)
#
Chapter:
Comparables
matches if the value is more than or equal to the provided value via the compareTo
function
range(from, to, fromInclusive=true, toInclusive=true)
#
Chapter:
Comparables
matches if the value is in range via the compareTo
function
and(left, right)
#
Chapter:
Combine matchers
combines two matchers via a logical and
or(left, right)
#
Chapter:
Combine matchers
combines two matchers via a logical or
not(matcher)
#
Chapter:
Combine matchers
negates the matcher
capture(slot)
#
Chapter:
Capture arguments to check later
captures a value to a CapturingSlot
capture(mutableList)
#
Chapter:
Capture arguments to check later
captures a value to a list
captureNullable(mutableList)
#
Chapter:
Capture arguments to check later
captures a value to a list together with null values
captureLambda()
#
Chapter:
Capture arguments to check later
captures a lambda
captureCoroutine()
#
Chapter:
Capture arguments to check later
captures a coroutine
invoke(...)
#
Chapter: TODO
calls a matched argument
coInvoke(...)
#
Chapter: TODO
calls a matched argument for a coroutine
hint(cls)
#
Chapter: TODO
hints the next return type in case it’s gotten erased
anyVararg()
#
Chapter: TODO
matches any elements in a vararg
varargAny(matcher)
#
Chapter: TODO
matches if any element is matching the matcher
varargAll(matcher)
#
Chapter: TODO
matches if all elements are matching the matcher
any...Vararg()
#
Chapter: TODO
matches any elements in vararg (specific to primitive type)
varargAny...(matcher)
#
Chapter: TODO
matches if any element is matching the matcher (specific to the primitive type)
varargAll...(matcher)
#
Chapter: TODO
matches if all elements are matching the matcher (specific to the primitive type)
A few special matchers available in verification mode only:
withArg { code }
#
Chapter:
Assertions with an argument
matches any value and allows to execute some code
withNullableArg { code }
#
Chapter:
Assertions with an argument
matches any nullable value and allows to execute some code
coWithArg { code }
#
Chapter: TODO
matches any value and allows to execute some coroutine code
coWithNullableArg { code }
#
Chapter: TODO
matches any nullable value and allows to execute some coroutine code
Validators #
verify { mock.call() }
#
Chapter: TODO
Do unordered verification that a call was performed
verify(inverse=true) { mock.call() }
#
Chapter: TODO
Do unordered verification that a call was not performed
verify(atLeast=n) { mock.call() }
#
Chapter: TODO
Do unordered verification that a call was performed at least n
times
verify(atMost=n) { mock.call() }
#
Chapter: TODO
Do unordered verification that a call was performed at most n
times
verify(exactly=n) { mock.call() }
#
Chapter: TODO
Do unordered verification that a call was performed exactly n
times
verifyAll { mock.call1(); mock.call2() }
#
Chapter: TODO
Do unordered verification that only the specified calls were executed for the mentioned mocks
verifyOrder { mock.call1(); mock.call2() }
#
Chapter: TODO
Do verification that the sequence of calls went one after another
verifySequence { mock.call1(); mock.call2() }
#
Chapter: TODO
Do verification that only the specified sequence of calls were executed for the mentioned mocks
verify { mock wasNot Called }
#
Chapter: TODO
Do verification that a mock was not called
verify { listOf(mock1, mock2) wasNot Called }
#
Chapter: TODO
Do verification that a list of mocks were not called
Answers #
An Answer can be followed up by one or more additional answers.
returns value
#
Chapter: TODO
specify that the matched call returns a specified value
returnsMany list
#
Chapter: TODO
specify that the matched call returns a value from the list, with subsequent calls returning the next element
throws ex
#
Chapter: TODO
specify that the matched call throws an exception
answers { code }
#
Chapter: TODO
specify that the matched call answers with a code block scoped with answer scope
coAnswers { code }
#
Chapter: TODO
specify that the matched call answers with a coroutine code block with answer scope
answers answerObj
#
Chapter: TODO
specify that the matched call answers with an Answer object
answers { nothing }
#
Chapter: TODO
specify that the matched call answers null
just Runs
#
Chapter: TODO
specify that the matched call is returning Unit (returns null)
propertyType Class
#
Chapter: TODO
specify the type of backing field accessor
nullablePropertyType Class
#
Chapter: TODO
specify the type of backing field accessor as a nullable type
Additional answer(s) #
A next answer is returned on each consequent call and the last value is persisted.
So this is similar to the returnsMany
semantics.
andThen value
#
Chapter: TODO
specify that the matched call returns one specified value
andThenMany list
#
Chapter: TODO
specify that the matched call returns value from the list, returning each time next element
andThenThrows ex
#
Chapter: TODO
specify that the matched call throws an exception
andThen { code }
#
Chapter: TODO
specify that the matched call answers with a code block scoped with answer scope
coAndThen { code }
#
Chapter: TODO
specify that the matched call answers with a coroutine code block with answer scope
andThenAnswer answerObj
#
Chapter: TODO
specify that the matched call answers with an Answer object
andThen { nothing }
#
Chapter: TODO
specify that the matched call answers null
Answer scope #
call
#
Chapter: TODO
a call object that consists of an invocation and a matcher
invocation
#
Chapter: TODO
contains information regarding the actual function invoked
matcher
#
Chapter: TODO
contains information regarding the matcher used to match the invocation
self
#
Chapter: TODO
reference to the object invocation made
method
#
Chapter: TODO
reference to the function invocation made
args
#
Chapter: TODO
reference to the arguments of invocation
nArgs
#
Chapter: TODO
number of invocation argument
arg(n)
#
Chapter: TODO
n-th argument
firstArg()
#
Chapter: TODO
first argument
secondArg()
#
Chapter: TODO
second argument
thirdArg()
#
Chapter: TODO
third argument
lastArg()
#
Chapter: TODO
last argument
captured()
#
Chapter: TODO
the last element in the list for convenience when capturing to a list
lambda<...>().invoke()
#
Chapter: TODO
call the captured lambda
coroutine<...>().coInvoke()
#
Chapter: TODO
call the captured coroutine
nothing
#
Chapter: TODO
null value for returning nothing as an answer
fieldValue
#
Chapter: TODO
accessor to the property backing field
fieldValueAny
#
Chapter: TODO
accessor to the property backing field with Any?
type
value
#
Chapter: TODO
value being set casted to same type as the property backing field
valueAny
#
Chapter: TODO
value being set with Any?
type
Vararg scope #
position
#
Chapter: TODO
the position of an argument in vararg array
nArgs
#
Chapter: TODO
overall count of arguments in vararg array