gpt4 book ai didi

java - 尝试使用 PowerMock 模拟静态系统类会给出 VerifyError

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:31:55 25 4
gpt4 key购买 nike

我正在尝试模拟 JOptionPane 静态方法,但卡在 Java.lang.VerifyError 中。所有版本都是最新的,因为我刚刚下载了适用于 Mockito 的 PowerMock 及其所有依赖项。

我不能使用对上一个问题给出的答案来包装类和子类化包装器——这在我们的应用程序中到处都是。目前,我已经在 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)

最佳答案

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

编辑:重新阅读问题,因为您正在尝试模拟 JOptionPane 本身,我不确定这是否有帮助,但也许您可以尝试使用排除模式。

关于java - 尝试使用 PowerMock 模拟静态系统类会给出 VerifyError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11943703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com