gpt4 book ai didi

java - Powermockito java.lang.VerifyError

转载 作者:行者123 更新时间:2023-12-03 02:29:29 25 4
gpt4 key购买 nike

我正在使用 powermock.mockstatic 这条线,这样我就可以控制它的返回值,因为它是唯一的 Swing ,我不必测试它。

@Before
public void setUp() throws Exception
RelatedIntelligencePanel rel = Mockito.mock(RelatedIntelligencePanel.class);
PowerMockito.mockStatic(RelatedIntelligencePanel.class);
PowerMockito.whenNew(RelatedIntelligencePanel.class).withNoArguments().thenReturn(rel);
...
.. some other unrelated code
}
...........
........ some other code and Tests
......
@Test
public class SomeClass{
RelatedIntelligencePanel relIntPanel = new RelatedIntelligencePanel();

但它抛出 java.lang.VerifyError。我曾经 mock 过很多这样的事情,没有一个异常(exception)。删除 @PrepareForTest 和 @Runwith 有帮助,但当我这样做时我会失去 powermock。我的详细错误也在下面;

java.lang.VerifyError: Bad return type
Exception Details:
Location:
javax/swing/plaf/metal/MetalLookAndFeel.getLayoutStyle()Ljavax/swing/LayoutStyle @3: areturn
Reason:
Type 'javax/swing/plaf/metal/MetalLookAndFeel$MetalLayoutStyle' (current frame, stack[0]) is not assignable to 'javax/swing/LayoutStyle' (from method signature)
Current Frame:
bci: @3
flags: { }
locals: { 'javax/swing/plaf/metal/MetalLookAndFeel'}
stack {'javax/swing/plaf/metal/MetalLookAndFeel$MetalLayoutStyle'}
Bytecode:
0x0000000: b807 49b0

最佳答案

好的,我找到答案了。同时使用 @PowerMockIgnore("javax.swing.*")

PowerMockito.mockStatic(ClassWithStatics.class);
when(ClassWithStatics.getString()).thenReturn("Hello!");

确实解决了我的问题。如果没有 @PowerMockIgnore("javax.swing.*")PowerMockito.when 将无法工作,反之亦然。 @stuXnet 和 @staszko032 都是正确的,但这些建议并不能单独发挥作用。注意:WhenNew 也可以工作,但在本例中不行。

关于java - Powermockito java.lang.VerifyError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49790103/

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