Skip to content

3.0.0

Choose a tag to compare

@lupuuss lupuuss released this 23 Nov 16:27
· 96 commits to master since this release
f478623

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 1

The 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 / verify misuses 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 an AutofillProvider to mock a method that accepts or returns a value class.
  • Now, it's allowed to call every and verify on 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 containsAll for Iterable and array types. Matches a collection in which all elements satisfy the predicate.
  • Added containsAny for Iterable and array types. Matches a collection in which any element satisfies the predicate.

⚠️ Breaking changes

  • ArgMatchersScope (renamed to MokkeryMatcherScope) no longer contains the method fun <T> matches(argType: KClass<*>, matcher: ArgMatcher<T>): T.
    Its usage should be replaced with the extension function fun <T> MokkeryMatcherScope.matches(matcher: ArgMatcher<T>): T.
  • The registration process for composite matchers has changed. The compose, isFilled, and assertFilled methods have been removed from ArgMatcher.Composite. Refer to the dev.mokkery.matcher.matchesComposite documentation 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.ext and dev.mokkery.templating.ctx APIs to mock methods with extension receivers or context parameters.

🐛 Bug fixes

  • #100 #96 Unexpected errors during templating

♻️ Deprecations

All deprecated functions have suggestions for replacement set up, so migration should be straightforward.

  • ArgMatchersScope is renamed to MokkeryMatcherScope.
  • All vararg matcher declarations are deprecated: VarArgMatcher, varargsAny, varargsAll, anyVarargs.
    They should be replaced with equivalent regular matchers.
  • eq is deprecated - it can be omitted.
  • neq is deprecated - replace with not.
  • eqRef is renamed to ref.
  • neqRef is deprecated - replace with not(ref(...)).
  • matching and matchingBy are renamed to matches and matchesBy.
  • Removed the deprecated FunctionScope API.

✨ Improvements

  • Compiler plugin is no longer applied to KotlinMetadataTarget.