我在使用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
的正确方法是什么?我可以在网上找到的所有示例都是针对verifyStatic
没有采用类参数的2.x.x之前的版本。 最佳答案
我认为PowerMockito版本不是问题。我用版本测试了以下代码
应用程序类:
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/