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
MertG
  • 393
  • 1
  • 4
  • 15

4 Answers4

7

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.

MertG
  • 393
  • 1
  • 4
  • 15
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?

stuXnet
  • 4,309
  • 3
  • 22
  • 31
  • 1
    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. – MertG Apr 12 '18 at 08:00
  • Can you provide a [SSCE](http://sscce.org/)? This would be of great help nailing this problem down. – stuXnet Apr 12 '18 at 16:42
  • 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. – MertG Apr 13 '18 at 11:59
  • But I tried to add more details to my code. I hope it helps. – MertG Apr 13 '18 at 12:03
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.

staszko032
  • 802
  • 6
  • 16
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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
DivDiff
  • 963
  • 2
  • 10
  • 22