I am experiencing an issue with mocking a static test with my code compiled with Java7.
I am annotating my jUnit test with the annotations
@RunWith(PowerMockRunner.class)
@PrepareForTest(StaticClassToMock.class)
When running my test and try to mock my static class with
PowerMockito.mockStatic(StaticClassToMock.class);
it returns
java.lang.VerifyError: JVMVRFY012 stack shape inconsistent [...]
If in StaticClassToMock I remove the Java7 constructs by substituting the catched exceptions in OR and putting them in cascade it works fine.
I saw that the last version of Powemock (1.6.6) is compiled with Java6.
Is my issue related to the Java7 constructs when PowerMock is compiled with Java6?
Thanks
That is the thing with PowerMock - welcome to its bizarre errors.
First question would be - are you using an IBM JDK? Because IBM JDK and PowerMock go even more "bizarre" than Oracle/OpenJDK and PowerMock.
If you do some search, there are plenty of potential hints around:
Anyway, the first answer would be: simply try if running your JVM using -noverify makes any difference.
The longer answer: unless you are testing 3rd party code which you can't change; consider ... not using static code in a way that makes you turn to PowerMock.
You see, static is first of all an abnormality to good OO design. It should be used with great care; as it puts a lot of direct coupling into your code. And simply spoken: using static is a one of the simpl ways to create code that is hard/impossible to test! So, if changing your code is an option, you could watch those videos to learn how to create testable code in the first place. And then your need to turn to PowerMock ... will simply vanish.
My personal two cents: I have spent many hours hunting down such PowerMock problems. Then we decided to do different designs that only allows for static content that does not break our ordinary unit testing. Since then we are living fine with EasyMock and Mockito. No more need for PowerMock; no more need to spend hours on debugging problems that had nothing to do with our production code; but only the mocking framework.