gpt4 book ai didi

java - 验证时 Powermock 的计数比实际调用的计数多

转载 作者:太空宇宙 更新时间:2023-11-04 13:21:06 24 4
gpt4 key购买 nike

这是 Powermock 的错误还是我做错了什么?

以下测试应该通过,但失败了:

trackBugPartialMockCountMore(com.xiaomi.finddevice.test.testcase.PowerMockBug)
org.mockito.exceptions.verification.TooManyActualInvocations:
classToMock.foo();
Wanted 1 time:
-> at com.xiaomi.finddevice.test.testcase.PowerMockBug.trackBugPartialMockCountMore(PowerMockBug.java:24)
But was 3 times. Undesired invocation:
-> at com.xiaomi.finddevice.test.testcase.PowerMockBug.trackBugPartialMockCountMore(PowerMockBug.java:22)

当我删除@PrepareForTest(ClassToMock.class)时,一切都很顺利,测试也通过了。

import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassToMock.class)
public class PowerMockBug {

@Test
public void trackBugPartialMockCountMore() {
ClassToMock mock = mock(ClassToMock.class);
when(mock.foo()).thenCallRealMethod();

mock.foo();

verify(mock).foo();
}

}


class ClassToMock {
public int foo() { return 0x10; }
}

版本:powermock-mockito-junit-1.6.3

最佳答案

在您的示例中,您不需要使用 PowerMock,因为您没有模拟/监视最终或静态方法。您可以安全地删除 @RunWith 和 @PrepareForTest 注释。仅需要mockito即可达到您的目的

关于java - 验证时 Powermock 的计数比实际调用的计数多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33051820/

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