mock/spy:创建一个类的模拟实例
stubbing:为以上创建出的模拟实例,对应的方法,打桩(即定制方法返回值)
这就是 powermock 框架的由来
<dependency>
<groupId>org.powermockgroupId>
<artifactId>powermock-module-junit4artifactId>
<version>2.0.2version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.powermockgroupId>
<artifactId>powermock-api-mockito2artifactId>
<version>2.0.2version>
<scope>testscope>
dependency>
//开启mock注解等功能
@RunWith(PowerMockRunner.class)
//需要mock的类的路径,如果mock static/final/private 必须指定
@PrepareForTest(fullyQualifiedNames = "com.wangkang.test.*")
public class CollaboratorWithFinalMethods {
public final String helloMethod() {
return "Hello World!";
}
}
CollaboratorWithFinalMethods mock = mock(CollaboratorWithFinalMethods.class);
whenNew(CollaboratorWithFinalMethods.class).withNoArguments().thenReturn(mock);
CollaboratorWithFinalMethods collaborator = new CollaboratorWithFinalMethods();
//验证是否调用了无参构造
verifyNew(CollaboratorWithFinalMethods.class).withNoArguments();
when(collaborator.helloMethod()).thenReturn("Hello Baeldung!");
//验证
String welcome = collaborator.helloMethod();
//验证调用了
Mockito.verify(collaborator).helloMethod();
//验证返回值
assertEquals("Hello Baeldung!", welcome);
public class CollaboratorWithStaticMethods {
public static String firstMethod(String name) {
return "Hello " + name + " !";
}
public static String secondMethod() {
return "Hello no one!";
}
public static String thirdMethod() {
return "Hello no one again!";
}
}
mockStatic(CollaboratorWithStaticMethods.class);
//可以:spy(CollaboratorForPartialMocking.class);
//stubbing
when(CollaboratorWithStaticMethods.firstMethod(Mockito.anyString())).thenReturn("Hello Baeldung!"); when(CollaboratorWithStaticMethods.secondMethod()).thenReturn("Nothing special");
//callRealMethod!!
assert CollaboratorWithStaticMethods.thirdMethod().equals("Hello no one again!"); when(CollaboratorWithStaticMethods.thirdMethod()).thenReturn("thirdFirstReturn", "thirdSecondReturn").thenThrow(new RuntimeException("dd"));
String firstWelcome1 = CollaboratorWithStaticMethods.firstMethod("dds");
String firstWelcome2 = CollaboratorWithStaticMethods.secondMethod();
assertEquals("Hello Baeldung!", firstWelcome1);
assertEquals("Nothing special", firstWelcome2);
//验证串联式打桩声明
assert CollaboratorWithStaticMethods.thirdMethod().equals("thirdFirstReturn");
assert CollaboratorWithStaticMethods.thirdMethod().equals("thirdSecondReturn");
try {
CollaboratorWithStaticMethods.thirdMethod();
Assert.fail();
} catch (Exception e) {
assert e.getMessage().equals("dd");
}
public class LuckyNumberGenerator {
public int getLuckyNumber(String name) {
saveIntoDatabase(name);
if (name == null) {
return getDefaultLuckyNumber();
}
return getComputedLuckyNumber(name.length());
}
private void saveIntoDatabase(String name) {}
private int getDefaultLuckyNumber() {
return 100;
}
private int getComputedLuckyNumber(int length) {
return length < 5 ? 5 : 10000;
}
}
//因为 mock 的是方法内调用的 private 方法,所以默认为 callRealMethod,仅对 private 方法,额外打桩
mock = mock(LuckyNumberGenerator.class, InvocationOnMock::callRealMethod);
when(mock, "getDefaultLuckyNumber").thenReturn(301);
assertEquals(301, mock.getLuckyNumber(null));
public class CollaboratorForPartialMocking {
public static String staticMethod() {
return "Hello Baeldung!";
}
public final String finalMethod() {
return "Hello Baeldung!";
}
private String privateMethod() {
return "Hello Baeldung!";
}
public String privateMethodCaller() {
return privateMethod() + " Welcome to the Java world.";
}
}
spy(CollaboratorForPartialMocking.class);
when(CollaboratorForPartialMocking.staticMethod()).thenReturn("I am a static mock method.");
returnValue = CollaboratorForPartialMocking.staticMethod();
//验证值
assertEquals("I am a static mock method.", returnValue);
CollaboratorForPartialMocking collaborator = new CollaboratorForPartialMocking();
CollaboratorForPartialMocking mock = spy(collaborator);
when(mock.finalMethod()).thenReturn("I am a final mock method.");
returnValue = mock.finalMethod();
assertEquals("I am a final mock method.", returnValue);
when(mock, "privateMethod").thenReturn("I am a private mock method.");
returnValue = mock.privateMethodCaller();
assertEquals("I am a private mock method. Welcome to the Java world.", returnValue);
github 源码地址
Introduction to PowerMock