Comments (7)
Hi @Krever
It sounds quite cool, extension methods was something a was thinking how to solve
What I don't get is what you mention about type inference, I never had issues with it using the standard mockito syntax, could you please provide a code snippet that shows the issue?
I agree with the verbosity of the verifications, and it would be great to find a way of simplifying them
Please do share the PoC, I'd more than happy to work with you in adding more and better features to the library
from mockito-scala.
Here is the link to the impl: https://gitlab.com/w-pitula/mockito4s
It's on gitlab cause I don't want to claim rather good name at this stage and confuse people.
It's rather simple but let me know if anything needs more explanation. We could also talk on gitter if a more dynamic form of communication is required.
What I don't get is what you mention about type inference, I never had issues with it using the standard mockito syntax, could you please provide a code snippet that shows the issue?
This is my bad, I was not clear enough. Original mockito syntax doesnt suffer from significant type inference problems. The only one I know is that any[T]
is inferred to be any[Nothing]
in many cases but I have not dealt with that in any way and I dont even have the example to prove it really happens.
The type inference problems show only when we want to introduce syntax similar to scala mock where we detach function call from its arguments like
(mock.method _).expects // scalamock
mock.expectsCall(_.method _) //my proposal
This detachment is required because mockito uses different ways to trigger setup and verification (to setup we call when(mock.method(args))
, to veirfy we call verify(mock).method(args)
.
With simple methods this kind of syntax it works well, but breaks in at least these 2 cases:
- when there are multiple arguments lists (especially implicits)
- when there are method overloads
In such a case one needs to provide types of all arguments so the compiler knows which extact method we want to call. To overcome this I propose the second syntax in which we specify arguments first and based on that compiler can successfully infer the method.
You can take a look into test to see how the syntax works. One limitation is that to get proper type inference in some case we use overload so methods have names like expects0
, expects1
and so on. I find it an acceptable compromise as I much more often change type signatures then function arity.
Additional comments: maybe it can be improved by using macros. I wanted to PoC sth very quickly and my experience with macros is rather limited, so all the "lib" is written in pure scala.
One more this that I reminded mysefl after looking into codebase: I originally planned to allow user to mix matchers with direct arguments passing (like method(1, any[String]). AFAIK this triggers error in mockito and should be quite easy to achieve by simply checking the type of each argument and wrap it in eq
matcher if necessary.
from mockito-scala.
All right, I'm taking a look at the code
In the meantime, it's worth to note 2 things
a) you're using Mockito 1.x which had several shortcomings to play with scala
b) any[T] from ArgumentsMatchersSugar solves the type inference anyway (partially by wrapping the call with a Scala call, and partially because it uses Mockito 2.x)
I still think that the syntax you propose could help with other scenarios like auto-verification, but I don't think it's necessary for the sake of type inference
from mockito-scala.
@Krever, thank you for feedback and suggestions!
The implicit verification is available with Mockito today when "strict stubs" are used. Have you seen this feature?
from mockito-scala.
No, apparently I have not, thanks! It feels like I might have missed a lot of mockito development.
Nevertheless, this looks awesome and much simpler then the workaround I came up with. It seems that for scala users (who typically use specs2 or scala test, not junit) MockitoSession
is a way to go, is that correct?
from mockito-scala.
@mockitoguy Thanks for the input!
@Krever do you want to create different issues for each one of the features you think are missing so we can tackle them one by one? also feel free to submit PRs if you have a working implementation for any of them already
from mockito-scala.
@bbonanno It feels like all the necessary features are already available. I will check it out in practice and create new issues if anything pops up.
The only feature I can think of is integration with testing frameworks to make enabling strict stubs easier. But its a completely separate thing from this issue.
Thank you guys for all your help!
from mockito-scala.
Related Issues (20)
- When Stubbing function call it throws cannot be cast to org.apache.spark.sql.Dataset java.lang.ClassCastException: HOT 3
- Have mockObject in async tests return a future HOT 2
- Session fails if tests override the default executionContext
- by-name parameters incorrectly evaluated when mocking HOT 1
- verify number of invocations with `times` is broken HOT 2
- 'this' is not available when debugging
- mockito-scala error `value thenAnswer is not a member of Nothing`
- Mockito won't stub method as expected if the method implemented with Singleton pattern.
- Mockito and Swagger - unsafe symbol ApiModelProperty (child of package annotations) in runtime reflection universe
- tag missing problem
- withObjectSpied call real method instead of stubbed HOT 1
- mockito-scala does not work with mockito 4.8.0 and above HOT 1
- Vulnerability CVE-2022-36944
- InvalidUseOfMatchersException on java 19 in presence of default arguments
- 'Illegal reflective access operation has occurred' using WithObjectMocked
- Misidentifies final overload as bridge, tries to override final method
- Cannot invoke "cats.effect.IO.map(scala.Function1)" because "fa" is null
- jdk21: use latest mockito-core as dependency for updated bytebuddy HOT 5
- Mockito cannot mock this class: class scala.xml.Elem HOT 1
- jdk21 support issue HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mockito-scala.