-
Notifications
You must be signed in to change notification settings - Fork 40.9k
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
Try to replace calls to Mockito internals #10352
Comments
All of our use of Mockito's internals in now isolated to /cc @TimvdLippe |
Hey, let's work together to avoid using internal APIs! Can you elaborate a bit on the use case? I've read |
@szczepiq Thanks. There's some background about |
@wilkinsona, I found this section of your comment very useful (#5837 (comment)): @Test
public void test() {
service.doSomething(99);
Mockito.verify(repository).save(99);
Mockito.verifyNoMoreInteractions(repository);
}
The reason Mockito needs to re-add verification mode is to correctly handle Mockito test spies. Example: calling verify(mockitoSpy).foo() will trigger real implementation of “foo” method. “foo” may delegate to other methods on the spy object, let’s say “bar”. We want to verify “foo” method, not “bar” method so we need to re-add verification of “foo”. Hope it explains why we do it this way in Mockito. Now let's figure out how help out Spring integration :) Here’s how I understand the Spring scenario, based on the code snippet above: Repository is a Spring proxy object (SP - spring proxy) wrapped with Mockito mock (MP - mockito proxy).
How can I better understand what happens in step 3) Why does the invocation object has different mock that the mock in verification mode? Does Spring do something interesting when delegating to Mockito proxy? Is there a unit test that demonstrates this behavior and easy steps to pull that code to IDE so that I can run it and reproduce the behavior? Thank you for useful links! Looking forward to fixing this internal use of Mockito API!!! |
@szczepiq Thanks for taking a look. I've created a minimal reproduction of the problem when calling |
Great! I will investigate it.
--
Szczepan Faber
Founder @ http://mockito.org | Twitter @ https://twitter.com/mockitoguy
Author @ https://www.linkedin.com/in/szczepiq/recent-activity/posts/
|
@wilkinsona, the sample project is fantastic! Thank you. I debugged it and I now I fully understand what's going on. Let me think about it a bit more and I'll come back with some options of growing Mockito public API to accommodate this use case. |
Dual proxy layer is an interesting caveat. There is no super clean solution :) Here’re the options we have I think:
mock(Foo.class, withSettings().verificationStartedListeners(new VerificationStartedListener() {
public void onVerifyStarted(VerificationStartedEvent event) {
event.getMock();
event.setMock(arbitrayObject);
}
}
VerificationState state = Mockito.framework().getVerificationState();
if (state.isVerificationStarted()) {
state.restartVerification(arbitraryObject)
}
My thoughts about options:
I'm excited we can grow Mockito API to streamline integrations!!! Thoughts about the APIs? |
Thank you, @szczepiq. Your second option sounds like it will work for us. When someone uses |
Fantastic!!! I'll put together a prototype soon. |
Since this is a code change in Mockito, is it ok to move the discussion to the Mockito ticket? I put together the prototype and documented it here: mockito/mockito#1191 (comment) Please take a look! I'm excited to make Mockito public APIs more robust! |
Here's a prototype of an update to Spring Boot to use the new Mockito API (and remove some other internal API usage in our tests): https://github.com/wilkinsona/spring-boot/tree/gh-10352. |
Mockito 2.11.0 (https://mvnrepository.com/artifact/org.mockito/mockito-core/2.11.0) has been released with the new API! |
Thank you, @mockitoguy and @TimvdLippe. We'll upgrade to Mockito 2.11 in 2.0 M6. |
Long term it would be nice to replace our calls to internal Mockito code to only use their public API (see this comment).
This should be easier once #10247 has been fixed.
The text was updated successfully, but these errors were encountered: