-
Notifications
You must be signed in to change notification settings - Fork 205
Enhance/unify and document stubbing API, more groundwork towards implementing improved suspend function support #556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
9a2d974 to
3011f9f
Compare
|
@jselbo Do you mind reviewing this one as well? |
… 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.
3011f9f to
d3240af
Compare
|
This PR has been rebased, updated and completed after recent merges. I consider it ready for review. |
|
It's a really big PR. Let me take a closer look tomorrow morning. |
|
Overall I think this is a good change and moves toward simplifying the API surface. I have some comments:
Just taking an example, how does this method benefit from having reified type if it does not use It's not a huge deal, but it breaks a handful of our tests that used generics for method stubbing. Example: Kotlin now says: The fix is easy at least, to make the helper inline+reified as well. But what's the reason for the change? All the tests still pass when removing the
I am fine with the alternative DSL-syntax, and it might be preferable for some (also matches MockK style), but I'm not keen on recommending it as objectively better than the classic stubbing syntax. I almost prefer to not blend the classic with the DSL style. So - Also, I know the goal is to make the |
| * } | ||
| * ``` | ||
| * | ||
| * This function acts as an alias for [Mockito.when], as `when` is a keyword in kotlin and as such |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I find some of these new docs are too wordy. I think the code examples are good though.
|
Thank you for reviewing and providing feedback. Really much appreciated. I did not have any intention to break any existing usages of whenever by adding inline/reified signature. I was under the impression that making the function inline/reified was improving Kotlin's capabilities to deduct the type T from the context on the call-side. But your experience is the contrary, so let me try to fix that. For now, the quick fix is to revert the change for the Then talking about lamda's versus clasic API. Actually the lambda based Wrapping a full test body in About the wordy documentation, sure I'm happy to shorten it. I will give it a try, but English is not my mother tongue. Is it yours? |
|
Let me also share, that I have not so much of real-world unit tests available in which mocks, suspend functions and/or value classes are applied. So I'm really happy that you share your experience with snapshot builds while doing testdrives against existing suits of test code. |
I saw the same problem with
I'm totally fine with your changes to prefer DSL syntax in the unit tests. My opinions are more about the public facing documentation and wikis. To be clear I support this PR and making
Thanks, it doesn't have to be perfect. But I think most docs can just refer to "corresponding mockito core API" instead of repeating details. |
|
I've reverted the infix/reified bit on the 2 functions mentioned. I hope this will make your test suites run again as before. And I have been taking out significant bits of the comments. Please have another look. Thanks. |
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:
whenever(mock.methodCall())/whenever(mock).methodCall()/whenever { mock.methodCall() }oron(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.wheneverBlocking { mock.methodCall() }andonBlocking { mock.methodCall() }have been declared deprecated to unify towards the abovementioned stubbing functionswhenever { mock.methodCall() }andon { mock.methodCall() }.onGeneric { mock.methodCall() }have been deprecated in favor ofon { mock.methodCall() }.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.:
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.