Powermockito java.lang.VerifyError
Asked Answered
J

4

5

I am using powermock.mockstatic this line blow so that I can control its returning value since its only swing I dont have to test it.

@Before
public void setUp() throws Exception
RelatedIntelligencePanel  rel = Mockito.mock(RelatedIntelligencePanel.class);
PowerMockito.mockStatic(RelatedIntelligencePanel.class);
PowerMockito.whenNew(RelatedIntelligencePanel.class).withNoArguments().thenReturn(rel);
...
.. some other unrelated code
}
...........
........ some other code and Tests
......
@Test
public class SomeClass{
RelatedIntelligencePanel relIntPanel = new RelatedIntelligencePanel();

But it throws java.lang.VerifyError. I did plenty of mocking this kind of thing and there was no exception like that. Removing @PrepareForTest and @Runwith helps but I lose powermock when I do that. My detailed error is also below ;

java.lang.VerifyError: Bad return type
Exception Details:
Location:
javax/swing/plaf/metal/MetalLookAndFeel.getLayoutStyle()Ljavax/swing/LayoutStyle @3: areturn
Reason:
Type 'javax/swing/plaf/metal/MetalLookAndFeel$MetalLayoutStyle' (current frame, stack[0]) is not assignable to 'javax/swing/LayoutStyle' (from method signature)
Current Frame:
bci: @3
flags: { }
locals: { 'javax/swing/plaf/metal/MetalLookAndFeel'}
stack {'javax/swing/plaf/metal/MetalLookAndFeel$MetalLayoutStyle'}
Bytecode:
0x0000000: b807 49b0
Justiciar answered 12/4, 2018 at 7:7 Comment(0)
J
8

Ok, I found the answer. Using both @PowerMockIgnore("javax.swing.*") and

PowerMockito.mockStatic(ClassWithStatics.class);
when(ClassWithStatics.getString()).thenReturn("Hello!");

did solve my problem. PowerMockito.when won't work without @PowerMockIgnore("javax.swing.*") and reverse is also true. Both @stuXnet and @staszko032 was correct but those advices didn't work alone. Note : WhenNew is also working but not in this case.

Justiciar answered 16/4, 2018 at 13:46 Comment(0)
I
0

There seems to be a problem with Powermock, @PrepareForTest and static methods.

Does annotating your test class with @PowerMockIgnore("javax.swing.*"), as commented on GitHub, help?

Inefficacy answered 12/4, 2018 at 7:17 Comment(4)
I also tried -noverify and -XX:-UseSplitVerifier but both didnt help too. If there is nothing I can do I might have to refactor the class so that I don't have to use PowerMockito but Its too much work so I want to avoid doing so.Justiciar
Can you provide a SSCE? This would be of great help nailing this problem down.Inefficacy
Other then what I wrote at my example only thing I can tell is I use @RunWith(PowerMockRunner.class) and @PrepareForTest({AccessServiceUtil.class, RelatedIntelligencePanel.class}) and In my code above staticMocked classes are not even using AccesServiceUtil.Justiciar
But I tried to add more details to my code. I hope it helps.Justiciar
M
0

This is no how you should mock a static with PowerMockito.

See this snippet (PowerMockito mock single static method and return object)

PowerMockito.mockStatic(ClassWithStatics.class);
when(ClassWithStatics.getString()).thenReturn("Hello!");

As you want to mock static method, you shouldn't use whenNew construction, maybe it leads to your error.

Mcfadden answered 12/4, 2018 at 7:18 Comment(1)
Since I am trying to mock some new creation I have to use whenNew with noArgs.Justiciar
W
0

I had a slightly different problem. I saw error complaining about a class in javax.net.* so I added javax.net.* to @PowerMockIgnore(...), and it resolved the issue for me. It seems as if you could simply ignore the problem package, and that will resolve your issue.

Wordsworth answered 11/9, 2020 at 11:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.