gpt4 book ai didi

java - PowerMockito VerifyStatic 在 2.0.0-beta5 中不起作用

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

我在使用 powermockito (2.0.0-beta5) 验证静态方法在调用不同(也是静态)方法时被调用了一定次数时遇到问题。这些类已准备好在我的测试文件的顶部进行测试相关的代码片段是:

mockStatic(Tester.class);
when(Tester.staticMethod(anyString(), anyString())).thenAnswer(new FirstResponseWithText());
OtherClass.methodThatCallsTesterStaticMethod("", "", "", false, "");
verifyStatic(Tester.class, times(3));
Tester.sendFaqRequest(anyString(), anyString());

FirstResponseWithText 是一个扩展控制响应顺序的 Answer 的类。我在别处使用过它,效果很好。

我在 verifyStatic 行收到以下错误:

org.mockito.exceptions.misusing.NotAMockException: 
Argument passed to verify() is of type Class and is not a mock!
Make sure you place the parenthesis correctly!
See the examples of correct verifications:
verify(mock).someMethod();
verify(mock, times(10)).someMethod();
verify(mock, atLeastOnce()).someMethod();

将类传递给 verifyStatic 的正确方法是什么?我可以在网上找到的所有示例都是针对 2.x.x 之前的版本,其中 verifyStatic 没有采用类参数。

最佳答案

我认为 PowerMockito 版本不是问题所在。我用版本测试了以下代码

  • 1.7.3,
  • 2.0.0-beta.5(您的版本),
  • 2.0.2.

应用类:

package de.scrum_master.stackoverflow.q52952222;

public class Tester {
public static String sendFaqRequest(String one, String two) {
return "real response";
}
}
package de.scrum_master.stackoverflow.q52952222;

public class OtherClass {
public static void methodThatCallsTesterStaticMethod(String one, String two, String three, boolean four, String five) {
System.out.println(Tester.sendFaqRequest("A", "B"));
System.out.println(Tester.sendFaqRequest("C", "D"));
System.out.println(Tester.sendFaqRequest("E", "F"));
}
}

测试类:

package de.scrum_master.stackoverflow.q52952222;

import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import java.util.Arrays;

public class FirstResponseWithText implements Answer {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
String methodName = invocation.getMethod().getName();
return methodName + " called with arguments: " + Arrays.toString(args);
}
}
package de.scrum_master.stackoverflow.q52952222;

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.anyString;
import static org.mockito.Mockito.times;
import static org.powermock.api.mockito.PowerMockito.*;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ Tester.class })
public class MyTest {
@Test
public void myTest() {
// Tell PowerMockito that we want to mock static methods in this class
mockStatic(Tester.class);
// Stub return value for static method call
when(Tester.sendFaqRequest(anyString(), anyString())).thenAnswer(new FirstResponseWithText());
// Call method which calls our static method 3x
OtherClass.methodThatCallsTesterStaticMethod("", "", "", false, "");
// Verify that Tester.sendFaqRequest was called 3x
verifyStatic(Tester.class, times(3));
Tester.sendFaqRequest(anyString(), anyString());
}
}

因此,如果这对您不起作用,并且您的 Maven 或 Gradle 依赖项也没有问题,那么区别可能在于您的 FirstResponseWithText 类。也许您想展示它,以便我们都能看到您在那里施展的魔法。正如您从我的示例代码中看到的那样,我不得不做出一些有根据的猜测,因为您只共享了一小段测试代码,而不是 MCVE。就像你应该的那样。这种方式我只能推测,实际上我不喜欢这样做,因为这可能对你和我自己都是浪费时间。

关于java - PowerMockito VerifyStatic 在 2.0.0-beta5 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52952222/

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