Comments (8)
@gursahibsahni There are quite a few things wrong with this, let's separate them in 2
- The project you uploaded to GitHub fails because maven resolves the wrong artifacts all around, similar to this bug, I had to manually add mockito-core and bytebuddy to make it run.
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.25.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.9.7</version>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>1.9.7</version>
</dependency>
- The test you wrote has some issues
- The cause of the exception you posted is that you can't create a
mock
while you are verifying (line 57), I guess you probably want something like
Mockito.verify(headerParamsResolverMock).resolve(any[NativeWebRequest])
HeaderParamsResolver
is being mocked properly, the fact that it looks like the real thing in the debugger is due to how theinline
mocker works- Line 50 should stub
false
(I guess this is a typo as in the code above it does that) - The last stubbing is never going to work as nothing is gonna be equal to a mock you create and dispose, again I guess you want
doReturn("string", Nil: _*).when(headerParamsResolverMock).resolve(any[NativeWebRequest])
- You still use pretty much the Java Mockito everywhere, if you were to use mockito-scala, your test would look more like
"resolverArgument" should {
"invoke resolve method if header parameter is not resolved" in {
val parameter = mock[Parameter]
doReturn(parameter).when(methodParameterMock).getParameter
doReturn("string").when(parameter).getName
doReturn(false).when(headerParamsResolverMock).isResolved
doReturn("string").when(headerParamsResolverMock).resolve(*)
irsArgumentResolver.resolveArgument(methodParameterMock, mock[ModelAndViewContainer], mock[NativeWebRequest], mock[WebDataBinderFactory])
verify(headerParamsResolverMock).resolve(*)
}
}
from mockito-scala.
@gursahibsahni Hi there, 2 things
- Could you create a tiny GitHub project so I can clone and reproduce the issue?
- Looking at your StackTrace and code it seems to me you are not using anything from mockito-scala (you are using
MockitoSugar
from Scalatest and theMockito
object from the core Java lib), would you mind switching to it and try? your issue may be one of the many already solved in the lib.
Thanks
from mockito-scala.
@bbonanno sorry about the confusion.
I have replaced MockitoSugar
of Scalatest
to Mockito
lib. Also, I have created a GIT repo here: https://github.com/gursahibsahni/debug-mockito-scala
The exception has changed and its a missing dependency
*** RUN ABORTED *** java.lang.NoClassDefFoundError: org/mockito/internal/invocation/InterceptedInvocation at org.mockito.internal.handler.ScalaMockHandler.handle(ScalaMockHandler.scala:23) at org.mockito.internal.handler.ScalaNullResultGuardian.handle(ScalaNullResultGuardian.scala:10) at org.mockito.internal.handler.InvocationNotifierHandler.handle(InvocationNotifierHandler.java:35) at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doIntercept(MockMethodInterceptor.java:65) at org.mockito.internal.creation.bytebuddy.MockMethodAdvice.handle(MockMethodAdvice.java:107)
from mockito-scala.
@bbonanno big thanks for addressing all the points. Especially the incorrect maven resolver.
All looks good and it feels so good writing verbose but less code for unit tests.
One thing I would bring up is that the shouldBe
matcher is not implicit from the docs that its inherited from org.scalatest.Matchers
. If you would like to have it on the README.md section of it, I can update it. Let me know your thoughts 👍
from mockito-scala.
@gursahibsahni No problem
You mean the examples in the tests that use shouldBe
?
If that I thought it was pretty obvious as most of the examples are written in Scalatest, but yeah, it would be nicer to actually use something agnostic of the test framework (as I'm soon adding support for Specs2), if you fancy raise a pull request on that
from mockito-scala.
@bbonanno I tried to raise a PR against release/1.x but I was denied permission to push. I couldn't find the contribution guidelines I can refer to.
from mockito-scala.
@gursahibsahni Is the standard flow, you have to fork the repo, push into that fork and raise a pull request from there to this repo.
from mockito-scala.
thanks for the instructions
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.