Trying to mock a static system class using PowerMock, VerifyError

I am trying to make fun of the static methods of JOptionPane and am stuck in Java.lang.VerifyError. All versions are current, since I just downloaded PowerMock for Mockito and all their dependencies.

I cannot use the answer asked for the previous question to wrap a class and subclass with a wrapper - this is all our application. At the moment, I have spent a lot of time on PowerMock, and I do not want to start with the festival.

Is there any way to resolve this error? I tried the instructions "Mocking Static Methods" and "Mocking System Classes". I cannot go further, since all I did was @RunWith (PowerMockRunner.class) and @PrepareForTest (My.class).

I found that I could reduce this to a problem with any reference to a class that extends JPanel. Here is the minimal test to create the problem (I got the same exception with PrepareForTest on Boffo and JPanel):

import javax.swing.JPanel; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) //@PrepareForTest(JPanel.class) @PrepareForTest(TestCase.Boffo.class) public class TestCase { @SuppressWarnings("serial") public static class Boffo extends JPanel {} @Test public void test() throws Exception { new Boffo(); } } 

Here is what I get when I try to run a test:

 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(Unknown Source) at javax.swing.UIManager.initializeDefaultLAF(Unknown Source) at javax.swing.UIManager.initialize(Unknown Source) at javax.swing.UIManager.maybeInitialize(Unknown Source) at javax.swing.UIManager.getUI(Unknown Source) at javax.swing.JPanel.updateUI(Unknown Source) at javax.swing.JPanel.<init>(Unknown Source) at javax.swing.JPanel.<init>(Unknown Source) at javax.swing.JPanel.<init>(Unknown Source) at com.package.TestCase$Boffo.<init>(TestCase.java:17) at com.package.TestCase.test(TestCase.java:21) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:312) 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:296) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:284) 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:209) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:148) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:122) 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:120) at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:102) at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53) at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:42) 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) 
+7
source share
2 answers

Here's a neat workaround / solution that my colleague opened today: Just add the @PowerMockIgnore("javax.swing.*") annotation @PowerMockIgnore("javax.swing.*") To the test class, and PowerMock will transfer the loading of the problematic classes to the system class loader.

edit: just re-read the question, and since you're trying to mock JOptionPane, I'm not sure if that will help, but maybe you can play around with the exception pattern.

+12
source

I have no idea why it throws a VerifyError , but you can overcome it by setting a fake LookAndFill before creating the class.

 public static class FakeLookAndFill extends BasicLookAndFeel { @Override public String getName() { return "FakeLookAndFill"; } @Override public String getID() { return "FakeLookAndFill"; } @Override public String getDescription() { return "FakeLookAndFill"; } @Override public boolean isNativeLookAndFeel() { return false; } @Override public boolean isSupportedLookAndFeel() { //note it returns true return true; } } @Before public void setUp() throws Exception { UIManager.setLookAndFeel(new FakeLookAndFill()); } 
+3
source

All Articles