-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Description
Hi guys,
I've taken a time for reviewing PowerMock code to find place where it use internal Mockito API. I'd like to discuss what could be done to avoid.
First think that I'd like to talk about it's MockMaker. PowerMock uses it custom MockMaker, which it past was used only for two things:
- cache a class create by CgLib
- return fake
InternalMockHandler
for static mocks.
The first case is no more actual, because ByteBuddy uses current context ClassLoader, so MockClassLoader
is used. But the second case still actual. But investigation shows that returning fake InternalMockHandler
is required only for one case right now.
Method org.mockito.internal.exceptions.Reporter.noMoreInteractionsWanted(Invocation undesired, List<VerificationAwareInvocation> invocations)
tries to safelyGetMockName(undesired.getMock())
. Finally, call comes to org.mockito.internal.util.MockUtilisMock(Object mock)
. It tries to get MockHandler
for mock, but it mock is static and it's not a PowerMockMaker, then null
is returned.
I was surprised that having PowerMockMaker
is required only for such thing. If speak, honestly, I don't have any idea, how it can be fixed on Mockito site. Maybe you have any thoughts?
The second point, it's a way how Mockito loads plugins. We had some discussion within #1006. Main point provided @podarsmarty (as for me main) that if there are two files org.mockito.plugins.MockMaker
in class path, then order of loading plugins are unpredictable. Such undetermined behaviour could introduce some fluky bugs, when for example one plugin specified in project and other in dependency jar. And for example it works, but later author of dependency jar make refactoring and change package or something like this. As result other plugin is loaded by Mockito and tests start failed.
For PowerMock it is issue, because is has own MockMaker
, which is required only if test runs with PowerMock. But, unfortunately, if PowerMock in class path it will be used always. If a developer wants to use mock-maker-inline
to be able to mock final without PowerMock, then it was impossible until PowerMock 1.7.0, where I added ability to specify MockMaker
to which PowerMockMaker
delegates calls. As for me, it will be good to have ability to separate custom MockMaker
and Mockito build-in MockMaker
. So custom MockMaker
could know which build-in MockMaker
should be used to delegate call if its required.