-
Notifications
You must be signed in to change notification settings - Fork 470
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
Stub creates Spy when a method is final #2039
Comments
The default mock maker can't mock final methods, you need to use the def converter = Stub(JwtAuthenticationConverter, mockMaker: MockMakers.mockito) {
convert(_ as Jwt) >> { println('I'm a stubbed method') }
} Please see documentation for more details about the different mock makers and their capabilities. Note: the You could also change the default used mock maker to |
@AndreasTu , thanks for the clarification. What do you think about it? |
@vprudnikov Not really, because how do we not that we mock a class with transitive final methods, because every class has final methods e.g. For example final classes the system would report such an error, but for final methods it is hard to know that it was intended or not. Also the Mocking final stuff e.g. classes and/or methods is a sad story. |
The docs even explicitly warn about it with an admonition reading
|
Mmm maybe we could throw an exception, when the user specifies an interaction on a final method, which will never trigger, if the mock maker did not support final methods. This would not prevent the general possible fault, but would ease the pain when someone does something like above. @Vampire What do you think? Nonetheless, the new exception would be a semantic break, but we could argue, that the old user code was already broken. |
Feel free to give it a try. |
Add error reporting code to the byte-buddy` mock maker to report interaction on final methods, which can't be handled. The IMockInteractionValidation interface allows IMockMakers to implement different kinds of validations on mock interactions. Fixes spockframework#2039
Add error reporting code to the byte-buddy` mock maker to report interaction on final methods, which can't be handled. The IMockInteractionValidation interface allows IMockMakers to implement different kinds of validations on mock interactions. Fixes spockframework#2039
Add error reporting code to the byte-buddy` mock maker to report interaction on final methods, which can't be handled. The IMockInteractionValidation interface allows IMockMakers to implement different kinds of validations on mock interactions. Fixes spockframework#2039
Add error reporting code to the byte-buddy` mock maker to report interaction on final methods, which can't be handled. The IMockInteractionValidation interface allows IMockMakers to implement different kinds of validations on mock interactions. Fixes spockframework#2039
Add error reporting code to the byte-buddy` mock maker to report interaction on final methods, which can't be handled. The IMockInteractionValidation interface allows IMockMakers to implement different kinds of validations on mock interactions. Fixes spockframework#2039
Add error reporting code to the byte-buddy` mock maker to report interaction on final methods, which can't be handled. The IMockInteractionValidator interface allows IMockMakers to implement different kinds of validations on mock interactions. Fixes spockframework#2039
I have some simple thoughts from a developer's perspective. And I hope you agree :)
Hope it makes sense :) |
@vprudnikov I fully agree as "developer's perspective", but IMHO this can't be implemented.
From the implementation perspective there is no distinction, and we can't make that distinction. Every Mock/Spy/Stub is under the hood the same, because the inner workings do that, is the same.
Sure, but the detail is a Java JVM thing, what we can't change.
So the answer of the If you want your behavior, please use the |
Describe the bug
Unexpectedly, a Stub turns into a Spy when creating it for a class with a final method.
To Reproduce
Try to create a Stub for
org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter
likeconverter#convert
method.Expected behavior
Either the method gets stubbed or an exception while creating a Stub saying that the stub cannot be created.
Actual behavior
A new spied instance of JwtAuthenticationConverter is created and the real
convert
method is called.Java version
21
Buildtool version
3.9.6
What operating system are you using
Windows
Dependencies
spock-spring:2.4-M1-groovy-4.0
Additional context
No response
The text was updated successfully, but these errors were encountered: