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
NotAMockException thrown by tests in different test classes due to separate classloader #593
Comments
I have the same bug in the exact same configuration |
I'm also experiencing this. Any workarounds the classloader issue would be greatly appreciated. I also discovered this stack overflow illustrating the same issue: From what I can tell, this is whats going on:
I've tried a couple of different hacks to try to get |
Same issue. Are there any solution or workaround so far? |
I found that for some classes, I could use Mockito.mock rather than PowerMockito.mock and it would work, even though I was mocking a final class. For other unit test classes, unless I explicitly need the robolectric stubbing I've switched to using the PowerkMock test runner. Between these two things I've managed to hack my way through, but it would be nice to have a real solution |
Please, can you try using this:
Bytheway I built a single little example that reproduces the problem (And is solved with the line above)
|
Some Observations with the above fix Even when using
The first Test class that uses the PowerMock rule executes as expected. Relevant build.gradle:
|
I can confirm that @fonkap's solution has fixed it for me. (I'm the poster of that SO question linked above.) |
This also solves the issue for me, although I ran into two things:
Thanks for the help, guys! I'm going to leave this open for now since this seems more like a workaround rather than a fix per se; if one of the PowerMock devs wants to consider this the officially sanctioned solution then feel free to close. |
I just want to add to confirm that adding " To add some more info, I experienced this a few days ago and changing the method name of the failing test caused the issue to go away (odd behaviour), but ultimately adding more tests brought it back. I had objenesis disabled the entire time as well and I have no idea what the method name had to do with the problem that was occurring. |
I'm running into a NotAMockException thrown from Mockito.verify() when using PowerMockito with Robolectric. It occurs when I'm trying to mock out parts of a third-party library; I've written some code to reproduce the issue.
Here are two classes that match the interface of the library I'm mocking:
And here are tests that reproduce the issue:
Both of these tests pass if run in isolation. However, if run together (with an IDE or with
gradle test
) then a NotAMockException is thrown byMockito.verify(mockAPI).doStuff();
.The order these are run in matters (which is why these tests classes are named TestB and TestC; if TestC were renamed to TestA then the issue disappears since apparently IntelliJ IDEA likes to run these in alphabetical order). I've spent some time poking around in the debugger, and what I found was that the following condition from CglibMockMaker.getHandler():
becomes true since
callback
andMethodInterceptorFilter
in this particular case were loaded by different instances ofMockClassLoader
, presumably due to the two instances ofPowerMockRule
.Finally: is this a PowerMock issue, or a Robolectric issue? I figured I'd start here due to the
PowerMockRule
/MockClassLoader
apparatus's involvement, but let me know if this is not the appropriate forum for this (or if it turns out this is just developer error :)The text was updated successfully, but these errors were encountered: