sujithps / powermock

Automatically exported from code.google.com/p/powermock
Apache License 2.0
0 stars 0 forks source link

java.lang.VerifyError, when using Swing components and @PrepareForTest #363

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Hello,

when I run a test, which has some Swing components and use @PrepareForTest, I 
get an Exception:
java.lang.VerifyError: (class: javax/swing/plaf/metal/OceanTheme, method: 
<clinit> signature: ()V) Bad type in putfield/putstatic

It appears only, when I use @PrepareForTest annotation and init some Swing 
components, without it's ok.

Is there some issue with Swing components using with mocking?

Thanks for the help,
Alexei

This throws Exception:

@RunWith( PowerMockRunner.class )
@PrepareForTest( { String.class } )
public class TestGUI
{
    @Test
    public final void test()
    {
        JLabel label = new JLabel( "foobar" );
    }
}

This runs fine:

@RunWith( PowerMockRunner.class )
public class TestGUI
{
    @Test
    public final void test()
    {
        JLabel label = new JLabel( "foobar" );
    }
}

Original issue reported on code.google.com by a.vermin...@gmail.com on 20 Jan 2012 at 4:47

GoogleCodeExporter commented 8 years ago
Hi

Are you using java7?

Original comment by johan.ha...@gmail.com on 19 Feb 2012 at 5:19

GoogleCodeExporter commented 8 years ago
Hi,

No, we are using Java 6 with 1.5 code compliance.

Original comment by a.vermin...@gmail.com on 19 Feb 2012 at 5:21

GoogleCodeExporter commented 8 years ago
We are running into this with Java 6, too, and it is preventing us from 
implementing a whole swath of tests. Any update? Anything info we can provide 
to assist?

Original comment by seangc...@gmail.com on 18 Apr 2012 at 8:47

GoogleCodeExporter commented 8 years ago
Hi, we are facing this issue since many days and eagerly looking for a fix to 
the above mentioned ticket. Is there any update regarding this ticket. Your 
update would really be helpful for us to proceed further.

Original comment by bharath...@gmail.com on 13 Jun 2012 at 7:31

GoogleCodeExporter commented 8 years ago
Are you using Java7?

Original comment by johan.ha...@gmail.com on 13 Jun 2012 at 7:43

GoogleCodeExporter commented 8 years ago
No, we are using Java6. Didn't tried it yet with Java7.

Original comment by a.vermin...@gmail.com on 13 Jun 2012 at 8:49

GoogleCodeExporter commented 8 years ago
What class are you preparing for test?

Original comment by johan.ha...@gmail.com on 13 Jun 2012 at 10:56

GoogleCodeExporter commented 8 years ago
By preparing for test, do you mean: @PrepareForTest( { String.class } )?? It 
doesn't matter which class get's prepared, it's always the same issue.

Original comment by a.vermin...@gmail.com on 14 Jun 2012 at 11:35

GoogleCodeExporter commented 8 years ago
Yes that's what I mean. Hmm are you sure that it doesn't matter? In the example 
in this issue it works fine if nothing is prepared (@PrepareForTest is not used 
at all).

Original comment by johan.ha...@gmail.com on 18 Jun 2012 at 6:18

GoogleCodeExporter commented 8 years ago
Yes, without @PrepareForTest it's fine. But we have test cases, where we want 
to mock GUI and also we need @PrepareForTest, because we mock some final or 
private methods...

Original comment by a.vermin...@gmail.com on 18 Jun 2012 at 10:11

GoogleCodeExporter commented 8 years ago
I understand. I strongly encourage to refactor your code and break-out the 
functionality into a new class that can be tested in isolation. In that way you 
probably don't even need PowerMock. For testing the UI interaction another 
framework should probably be just, fest-swing or something similar.

Original comment by johan.ha...@gmail.com on 18 Jun 2012 at 10:24

GoogleCodeExporter commented 8 years ago
The PrepareForTest annotation doesn't even need to specify any classes (but it 
needs to be present), for the VerifyError to be thrown - see example below. 
This is with the latest versions of mockito, powermock-module-junit4 and 
powermock-api-mockito (and their Maven dependencies).

A question was asked regarding which version of Java - is this fixed in Java 7 
then?

@RunWith(PowerMockRunner.class)
@PrepareForTest // Don't even need to specify any classes to prepare!
public class VerifyErrorTest {

    @Test
    public void test() {
        new JFrame();
    }

}

Original comment by matt.s.w...@gmail.com on 8 Feb 2013 at 12:15

GoogleCodeExporter commented 8 years ago
PrepareForTest automatically prepares your test case so that might be what's 
causing it. 

Original comment by johan.ha...@gmail.com on 8 Feb 2013 at 3:58

GoogleCodeExporter commented 8 years ago
Have just tried this in Java 7 (v1.7.0_45) and it's still not fixed :-(

Original comment by matt.s.w...@gmail.com on 21 Jan 2014 at 11:49

GoogleCodeExporter commented 8 years ago
Here's a neat workaround/solution that a colleague of mine discovered today: 
Just add the annotation @PowerMockIgnore("javax.swing.*") to the test class and 
PowerMock will defer the loading of the problematic classes to the system 
classloader.

Original comment by matt.s.w...@gmail.com on 21 Jan 2014 at 4:20

GoogleCodeExporter commented 8 years ago
I changed the project system library to j2se-1.5 to fix this. There may be a 
problem with the jre you are using.

Original comment by fmayrand...@gmail.com on 8 Oct 2014 at 8:05