3.0.0
Changelog
This major update introduces a full refactor of the templating mechanism - everything that happens inside every and verify blocks.
It addresses a wide range of bugs that previously caused unexpected errors when setting up a behavior for a method,
and it now provides compile-time validation for these blocks.
Also, this version deprecates vararg matchers. Now, any matcher that accepts array can be used with spread operator (*).
// Mokkery 2
every { mock.callWithVarargs(1, *anyIntVarargs(), 10) } returns 1
every { mock.callWithVarargs(1, *varargsIntAll { it % 2 == 0 }, 10) } returns 1
// Mokkery 3
every { mock.callWithVarargs(1, *any(), 10) } returns 1
every { mock.callWithVarargs(1, *containsAllInts { it % 2 == 0 }, 10) } returns 1The new approach allows you to easily create custom matchers and use them with the spread operator:
every { mock.callWithVarargs(1, *matches { it.size > 2 }, 10) } returns 1
// Matches a call with varargs that starts with 1, ends with 10, and has at least two elements in between.🚀 Features
- Most
every/verifymisuses are now reported at compile time. - Full support for default arguments.
Previously, only methods with constant default values could be mocked.
Now, Mokkery correctly verifies default values computed from other arguments, provided these computations are deterministic.
If the default value is non-deterministic (e.g., random), it cannot be verified because it’s not possible to predict the correct value for the call. - Literals can be mixed with matchers in any scenario - for example, when passed to composite matchers.
- Support for methods with context parameters.
- Removes the WASM value classes limitation.
It is no longer necessary to set up anAutofillProviderto mock a method that accepts or returns a value class. - Now, it's allowed to call
everyandverifyon the same mock in parallel. - Matchers can be extracted to variables.
- It’s now possible to use any matcher that accepts an array as a vararg matcher (with the spread operator).
- Added
containsAllforIterableand array types. Matches a collection in which all elements satisfy the predicate. - Added
containsAnyforIterableand array types. Matches a collection in which any element satisfies the predicate.
⚠️ Breaking changes
ArgMatchersScope(renamed toMokkeryMatcherScope) no longer contains the methodfun <T> matches(argType: KClass<*>, matcher: ArgMatcher<T>): T.
Its usage should be replaced with the extension functionfun <T> MokkeryMatcherScope.matches(matcher: ArgMatcher<T>): T.- The registration process for composite matchers has changed. The
compose,isFilled, andassertFilledmethods have been removed fromArgMatcher.Composite. Refer to thedev.mokkery.matcher.matchesCompositedocumentation and existing implementations for the new approach to implementing composite matchers. - Scope functions can no longer be used to call mock methods inside templating blocks. Use the new
dev.mokkery.templating.extanddev.mokkery.templating.ctxAPIs to mock methods with extension receivers or context parameters.
🐛 Bug fixes
♻️ Deprecations
All deprecated functions have suggestions for replacement set up, so migration should be straightforward.
ArgMatchersScopeis renamed toMokkeryMatcherScope.- All vararg matcher declarations are deprecated:
VarArgMatcher,varargsAny,varargsAll,anyVarargs.
They should be replaced with equivalent regular matchers. eqis deprecated - it can be omitted.neqis deprecated - replace withnot.eqRefis renamed toref.neqRefis deprecated - replace withnot(ref(...)).matchingandmatchingByare renamed tomatchesandmatchesBy.- Removed the deprecated
FunctionScopeAPI.
✨ Improvements
- Compiler plugin is no longer applied to
KotlinMetadataTarget.