PowerMock
官方文档:https://github.com/powermock/powermock/wiki
一、Mock工具
在做ut时,发现要测试的方法会引用很多外部依赖的对象,就可能需要用Mock工具来模拟这些外部依赖的对象,来完成ut。
二、PowerMock
在EasyMock、Mockito等的基础上,增加了对static、final、private等方法的mock。
1、引入依赖
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
版本可根据具体情况而定。
2、注解
PowerMock有两个十分重要的注解:
@Runwith(PowerMockRunner.class)
@prepareForTest({classA.class, classB.class…})
两个注解需要配合使用,其中@prepareForTest中添加的是在test过程中mock的类
还有其他注解,例如
- @PowerMockRunnerDelegate()
支持将测试执行委托给另一个JUnit runner而不使用JUnit Rule. 这将实际的测试执行留给您选择的另一个runner。 例如,测试可以委托给“SpringJunit4ClassRunner”,“Parameterized”或“Enclosed”runner。具体用法可自行查阅相关文档。
- @Rule
在使用@prepareForTest注解时,若加上要测试的本类,jacoco无法统计到覆盖率,此时可以尝试使用@Rule替换。此注解需要引入对应依赖
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-classloading-xstream</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
使用参考:
@PrepareForTest(X.class);
public class MyTest {
@Rule
public PowerMockRule rule = new PowerMockRule();
// Tests goes here
...
}
3、基本用法
(1)普通mock(mock new出来的对象)
- 有返回值的方法
File file = new File(MY_PATH);
if (file.exist()) {
// do sth...
}
File file = PowerMockito.mock(File.class);
PowerMockito.whenNew(File.class).withArguments(Mockito.anyString()).thenReturn(file);
PowerMockito.doReturn(true).when(file).exists();
// PowerMockito.when(file.exists()).thenReturn(true);
- 无返回值的方法
MockClass mockClass = new MockClass();
mockClass.run();
MockClass mockClass = PowerMockito.mock(MockClass.class);
PowerMockito.whenNew(MockClass.class).withNoArguments().thenReturn(mockClass);
PowerMockito.doNothing().when(mockClass).run();
- 其他
InterfaceB interfaceB = new ClassA();
ClassC classC = ClassD.getInstance().methodD();
ClassA classA = PowerMockito.mock(ClassA.class);
PowerMockito.whenNew(ClassA.class).withNoArguments().thenReturn(classA);
PowerMockito.mockStatic(ClassD.class);
ClassD classD = PowerMockito.mock(ClassD.class);
PowerMockito.when(ClassD.getInstance()).thenReturn(classD);
ClassC classC = PowerMockito.mock(ClassC.class);
PowerMockito.when(classD.methodD()).thenReturn(classC());
(2)mock void方法
-
doNothing()
-
doThrow()
PowerMockito.doThrow(newRuntimeException()).when(mockClass).update(anyString(), anyInt()); PowerMockito.doNothing().doThrow(newRuntimeException()).when(mockClass).update(anyString(), anyInt());
-
doAnswer()
-
doCallRealMethod()
PowerMockito.doCallRealMethod().when(mockClass).update(anyString(), anyInt());
(3)mock static方法
- 有返回值
public class MockClass {
public static boolean methodstatic() {
// do sth...
}
}
PowerMockito.mockStatic(MockClass.class);
PowerMockito.when(MockClass.methodstatic()).thenReturn(something);
- 无返回值 --> 目前只想到直接suppress跳过执行,待补充
PowerMockito.mockStatic(MockClass.class);
PowerMockito.suppress(PowerMockito.method(MockClass.class, "methodstatic"));
(4)mock final方法
public class MockClass {
public final boolean methodFinal() {
// do sth...
}
}
MockClass mockClass = PowerMockito.mock(MockClass.class);
PowerMockito.when(mockClass.methodFinal()).thenReturn(something);
(5)mock private方法
public class MockClass {
private boolean methodFinal() {
// do sth...
}
}
PowerMockito.when(MockClass, "methodFinal").thenReturn(something);
(6)mock 单例类
String result = MockClass.getInstance().run("run");
PowerMockito.mockStatic(MockClass.class);
// Powermock mock出单例类
MockClass mockClass = PowerMockito.mock(MockClass.class);
// 给单例类的getInstance方法打桩
PowerMockito.when(MockClass.getInstance()).thenReturn(mockClass);
// 对mock类queryPerson的方法打桩
PowerMockito.when(mockClass.run(anyString())).thenReturn(anyString());
(7)跳过方法执行
PowerMockito.mockStatic(MockClass.class);
PowerMockito.suppress(PowerMockito.method(MockClass.class, "mockMethod"));
参考文档:
https://www.softwaretestinghelp.com/mock-private-static-void-methods-mockito/
https://www.zhuxianfei.com/java/50271.html文章来源:https://www.toymoban.com/news/detail-623576.html
https://blog.csdn.net/jackiehff/article/details/14000779文章来源地址https://www.toymoban.com/news/detail-623576.html
到了这里,关于PowerMock的简单使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!