gpt4 book ai didi

java - PowerMock mockStatic 没有捕获模拟的静态 void 方法调用

转载 作者:行者123 更新时间:2023-11-30 08:18:47 29 4
gpt4 key购买 nike

我尝试使用 PowerMock 而不是 Mockito 来模拟静态 void 方法,但效果不太好。

我的示例代码:

BlackTempleTest.java

package com;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.BlackTempleTest.Illidan;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ Illidan.class, EvilBrother.class })
public class BlackTempleTest {

Answer<Object> defaultAnswer = new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
System.out.println("Haha!");
return null;
}
};

@Test(expected = AssertionError.class)
public void testIllidanFight() throws Exception {
Illidan.startFight();
}

@Test
public void testCheatOnIllidanFight() throws Exception {
PowerMockito.mockStatic(Illidan.class, defaultAnswer);

Illidan.startFight();
}

@Test(expected = AssertionError.class)
public void testEvilBrotherFight() throws Exception {
EvilBrother.startFight();
}

// dont work
@Test
public void testCheatOnEvilBrotherFight() throws Exception {
PowerMockito.mockStatic(EvilBrother.class, defaultAnswer);

EvilBrother.startFight();
}

static class Illidan {
static void startFight() {
Assert.fail("You are not prepared!");
}
}
}

邪恶兄弟.java

package com;

import com.BlackTempleTest.Illidan;

public class EvilBrother extends Illidan {

}

我的问题是,嵌套类按预期使用 @PrepareForTest 和 PowerMockito.mockStatic 的组合进行模拟,但如果该类位于其自己的类文件中,这些语句将不起作用。

如何修复此测试?

编辑:

    PowerMockito.doAnswer(defaultAnswer).when(EvilBrother.class, "startFight");

可以通过 Powermock 管道传输错误调用,但会执行 Assert.fail。

java.lang.AssertionError: You are not prepared!
at org.junit.Assert.fail(Assert.java:88)
at com.BlackTempleTest$Illidan.startFight(BlackTempleTest.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1873)
at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:773)
at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:753)
at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:466)
at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:106)
at com.BlackTempleTest.testCheatOnEvilBrotherFight(BlackTempleTest.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

最佳答案

解决方案是,即使你调用 EvilBrother.startFight,你也必须模拟 Illidan 而不是 EvilBrother,因为该方法是继承的。

@Test
public void testCheatOnEvilBrotherFight() throws Exception {
PowerMockito.mockStatic(Illidan.class, defaultAnswer);

EvilBrother.startFight();
}

关于java - PowerMock mockStatic 没有捕获模拟的静态 void 方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29275179/

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