尝试使用PowerMock模拟静态系统类会产生VerifyError

时间:2012-08-13 23:30:35

标签: java swing unit-testing junit powermock

我正在尝试模拟JOptionPane静态方法,并且遇到了Java.lang.VerifyError。所有版本都是最新版本,因为我刚下载了PowerMock for Mockito及其所有依赖项。

我无法使用前一个问题给出的答案来包装类和子类包装器 - 这完全在我们的应用程序中。此时我在PowerMock上投入了相当多的时间,我不想重新开始。

有没有办法解决这个错误?我在指令中尝试了“模拟静态方法”和“模拟系统类”。我不能再往前走,因为我所做的就是@RunWith(PowerMockRunner.class)和@PrepareForTest(My.class)。

我发现我可以通过对扩展JPanel的类的任何引用来解决这个问题。这是创建问题的最小测试(我在Boffo和JPanel上使用PrepareForTest得到了相同的例外):

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();
    }
}

以下是我尝试运行测试时遇到的异常:

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)

2 个答案:

答案 0 :(得分:16)

这是我的一位同事今天发现的一个简洁的解决方法/解决方案: 只需将注释@PowerMockIgnore("javax.swing.*")添加到测试类中,PowerMock就会将有问题的类的加载推迟到系统类加载器。

编辑:重新阅读问题,因为你试图模仿JOptionPane本身我不确定这会有所帮助,但也许你可以使用排除模式。

答案 1 :(得分:3)

我实际上不知道它为什么抛出VerifyError,但你可以通过在实例化类之前提前设置假LookAndFill来克服它。

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());
}