Skip to content

Conversation

@m-koops
Copy link
Contributor

@m-koops m-koops commented Dec 6, 2025

This PR is another step of improving suspend function support in Mockito-Kotlin as discussed in #553.

This PR specifically focusses on documenting the stubbing API in Mockito-Kotlin, as well as unifying the various stubbing methods towords a unified model:

  • to start a stubbing operation with whenever(mock.methodCall()) / whenever(mock).methodCall() / whenever { mock.methodCall() } or on(mock.methodCall()) / on { mock.methodCall() }; these methods supply universal support for synchronous and suspendable methods/functios, as well as support for methods/function stubs with generics return types.
  • specific stubbing functions for suspendable functions, e.g. wheneverBlocking { mock.methodCall() } and onBlocking { mock.methodCall() } have been declared deprecated to unify towards the abovementioned stubbing functions whenever { mock.methodCall() } and on { mock.methodCall() }.
  • Likewise the stubbing function onGeneric { mock.methodCall() } have been deprecated in favor of on { mock.methodCall() }.
  • Some stubbing functions have been enhanced to infix functions and/or inline function with reified generic type to provide easier/cleaner to use stubbing API to the users of this library.

The unit tests now tend more towards applying a notation with lambdas for specifying methodCalls and answer specification, using infix notation where posible, e.g.:

        val mock = mock<SynchronousFunctions> {
           on { stringResult() } doAnswer { "result" }
       }

This signifies a move away from mere aliasing/casting functions on top of classic Mockito syntax, towards a more idiomtic Kotlin syntax to stub mocks. More prominent application of lamdas allows us to let the stubbing API evolve in the direction of a clean mocking DSL. This universal idiomtic Kotlin syntax is fully leveraging the powers of lambdas, infix functions, and reified generics in inline functions.

But of course, all alternative notations still apply and are covered with unit tests as well to demonstrate these alternative notations.

@m-koops m-koops force-pushed the document-stubbing-api branch from 997931f to 9a2d974 Compare December 6, 2025 13:34
… apply whenever() with lambda arg as default in test cases). Also, deprecated methods onBlocking(methodCall), onGeneric(methodCall), wheneverBlocking(methodCall) to unify towards whenever(methodCall) and on(methodCall) as universal methods to stub any method call, being either synchronous, suspendable or with generics return type.
@m-koops m-koops force-pushed the document-stubbing-api branch from 9a2d974 to 3011f9f Compare December 7, 2025 20:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant