How to mock a Swing Application in a JUnit test?

2,017 views
Skip to first unread message

Fabien

unread,
Dec 1, 2010, 6:06:36 AM12/1/10
to PowerMock
Hello eveybody,

I would like to create a JUnit test of a JPanel class.
I am using the version 1.4.6 of PowerMock and the version 1.8.5 of
Mockito.

Please find my JUnit source code:

@RunWith(PowerMockRunner.class)
@PrepareForTest(GraphicsEnvironment.class)
public class MainFrameControllerTest {
@Test
public void init() {
PowerMockito.mockStatic(GraphicsEnvironment.class);
Mockito.when(GraphicsEnvironment.isHeadless()).thenReturn(false);
new MainFrameController();
}
}

As you can see I would like to mock the isHeadless() function of the
GraphicsEnvironment class. In my test, I am creating the controller
which will launch the view of my application.

When I launch the test, I have this following stack trace:

java.lang.VerifyError: (class: javax/swing/plaf/metal/
MetalLookAndFeel, method: getLayoutStyle signature: ()Ljavax/swing/
LayoutStyle;) Wrong return type in function
at javax.swing.UIManager.setLookAndFeel(UIManager.java:554)
at javax.swing.UIManager.initializeDefaultLAF(UIManager.java:1317)
at javax.swing.UIManager.initialize(UIManager.java:1406)
at javax.swing.UIManager.maybeInitialize(UIManager.java:1394)
at javax.swing.UIManager.getUI(UIManager.java:980)
at javax.swing.JMenu.updateUI(JMenu.java:206)
at javax.swing.JMenuItem.init(JMenuItem.java:194)
at javax.swing.JMenuItem.<init>(JMenuItem.java:133)
at javax.swing.JMenuItem.<init>(JMenuItem.java:110)
at javax.swing.JMenu.<init>(JMenu.java:158)
at javax.swing.JMenu.<init>(JMenu.java:181)
at
com.perso.framework.client.hmi.mainframe.menu.rootmenu.RootMenuView.<init>(RootMenuView.java:
88)
at
com.perso.framework.client.hmi.mainframe.menu.rootmenu.RootMenuController.<init>(RootMenuController.java:
59)
at
com.perso.framework.client.hmi.mainframe.menu.MenuController.<init>(MenuController.java:
73)
at
com.perso.framework.client.hmi.mainframe.frame.MainFrameController.<init>(MainFrameController.java:
76)
at
com.perso.framework.client.hmi.mainframe.frame.MainFrameControllerTest.init(MainFrameControllerTest.java:
54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:
322)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:
86)
at
org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:
94)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:
309)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl
$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:
112)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl
$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:
73)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:
297)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:
84)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:
222)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:
161)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$1.run(PowerMockJUnit44RunnerDelegateImpl.java:135)
at
org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:
34)
at
org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:
44)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:
133)
at
org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:
112)
at
org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:
57)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:
46)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:
38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
467)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
683)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:
390)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:
197)


I don't know how I can fix my problem... If sombody has an idea???

Jean-Christophe DEROUET

unread,
Dec 1, 2010, 6:49:09 AM12/1/10
to powe...@googlegroups.com
Not sure from what you posted, but maybe you have to prepare for test, mock and stub the other class UIManager as well? 

2010/12/1 Fabien <fabien....@gmail.com>

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To post to this group, send email to powe...@googlegroups.com.
To unsubscribe from this group, send email to powermock+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/powermock?hl=en.


Johan Haleby

unread,
Dec 1, 2010, 7:01:07 AM12/1/10
to powe...@googlegroups.com
I think generally you should avoid testing the GUI in this way. There are good tools for Swing UI testing such as fest-swing that you can have a look at. Or do you have a special reason for why you want to mock the GUI code?

/Johan

Fabien

unread,
Dec 1, 2010, 8:16:40 AM12/1/10
to PowerMock
Yes I have a special reason for mocking GUI code.
I use Cobertura as Java code coverage analysis tool and to improve my
results, I would like to do some JUnits tests for the GUI code.
In my test, I just want to initialize the main controller of my
application which initializes all views of my code.
With this test, the coverage percent will be improved.
Note that my JUnit tests are running on a server on which the X11
display is not activated. That why I would like to mock the
isHeadless() method.
Concerning the exception, do you have any idea?

I am going to check fest library...

On Dec 1, 1:01 pm, Johan Haleby <johan.hal...@gmail.com> wrote:
> I think generally you should avoid testing the GUI in this way. There are
> good tools for Swing UI testing such as
> fest-swing<http://code.google.com/p/fest/> that
> you can have a look at. Or do you have a special reason for why you want to
> mock the GUI code?
>
> /Johan
>
> On Wed, Dec 1, 2010 at 12:49 PM, Jean-Christophe DEROUET <
>
> jc.dero...@gmail.com> wrote:
> > Not sure from what you posted, but maybe you have to prepare for test, mock
> > and stub the other class UIManager as well?
>
> > 2010/12/1 Fabien <fabien.duca...@gmail.com>
> >> powermock+...@googlegroups.com<powermock%2Bunsu...@googlegroups.com>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/powermock?hl=en.
>
> >  --
> > You received this message because you are subscribed to the Google Groups
> > "PowerMock" group.
> > To post to this group, send email to powe...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > powermock+...@googlegroups.com<powermock%2Bunsu...@googlegroups.com>
> > .

Johan Haleby

unread,
Dec 1, 2010, 8:27:23 AM12/1/10
to powe...@googlegroups.com
Not really sure about the exception but it could be that PowerMock is not allowed to modify classes loaded from the javax package, perhaps you need to mock it using this approach.

/Johan

To unsubscribe from this group, send email to powermock+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages