jbi*_*ird 11 java junit mockito powermockito
我在使用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());
Run Code Online (Sandbox Code Playgroud)
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();
Run Code Online (Sandbox Code Playgroud)
将课程传递给的正确方法是什么verifyStatic?我在网上可以找到的所有示例都是针对2.xx之前的版本,其中verifyStatic没有类参数。
我认为PowerMockito版本不是问题。我用版本测试了以下代码
应用类:
package de.scrum_master.stackoverflow.q52952222;
public class Tester {
public static String sendFaqRequest(String one, String two) {
return "real response";
}
}
Run Code Online (Sandbox Code Playgroud)
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"));
}
}
Run Code Online (Sandbox Code Playgroud)
测试类:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
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());
}
}
Run Code Online (Sandbox Code Playgroud)
因此,如果这对您不起作用并且您的 Maven 或 Gradle 依赖项也可以,那么差异可能在于您的FirstResponseWithText类中。也许你想展示它,以便我们都能看到你在那里施展了什么样的魔法。正如您从我的示例代码中看到的,我必须做出一些有根据的猜测,因为您只共享一小部分测试代码,而不是像您应该的那样共享MCVE。这样我只能推测,实际上我不喜欢这样做,因为这可能对你和我自己来说都是浪费时间。