引入如下的Mock jar包:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.0-RC.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.0-RC.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
普通Mock: Mock参数传递的对象
// 测试类
public class FlySunDemo {
public boolean callArgumentInstance(File file) {
return file.exists();
}
}
public class FlySunMockTest {
@Test
public void testCallArgumentInstance(){
//mock出入参File对象
File file = PowerMockito.mock(File.class);
FlySunDemo demo = new FlySunDemo();
PowerMockito.when(file.exists()).thenReturn(true);
Assert.assertTrue(demo.callArgumentInstance(file));
}
}
注:普通Mock不需要加@RunWith和@PrepareForTest注解。
Mock方法内部new出来的对象
// 测试代码
public class FlySunDemo {
public boolean callArgumentInstance(String path) {
File file = new File(path);
return file.exists();
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest(FlySunDemo.class)
public class FlySunMockTest {
@Test
public void testCallArgumentInstance(){
File file = PowerMockito.mock(File.class);
try {
PowerMockito.whenNew(File.class).withArguments(“bbb”).thenReturn(file);
FlySunDemo demo = new FlySunDemo();
PowerMockito.when(file.exists()).thenReturn(true);
Assert.assertTrue(demo.callArgumentInstance(“bbb”));
} catch (Exception e) {
e.printStackTrace();
}
}
}
注:当使用PowerMockito.whenNew方法时,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是需要mock的new对象代码所在的类。
Mock普通对象的final方法
//测试代码
public class ClassDependency {
public final boolean isAlive() {
return false;
}
}
public class FlySunDemo {
public boolean callFinalMethod(ClassDependency refer) {
return refer.isAlive();
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassDependency.class)
public class FlySunMockTest {
@Test
public void testCallFinalMethod() {
ClassDependency refer = PowerMockito.mock(ClassDependency.class);
PowerMockito.when(refer.isAlive()).thenReturn(true);
FlySunDemo demo = new FlySunDemo();
Assert.assertTrue(demo.callFinalMethod(refer));
}
}
注: 当需要mock final方法的时候,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是final方法所在的类。
Mock普通类的静态方法
// 测试代码
public class ClassDependency {
public static boolean isAlive() {
return false;
}
}
public class FlySunDemo {
public boolean callStaticMethod() {
return ClassDependency.isAlive();
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassDependency.class)
public class FlySunMockTest {
@Test
public void testCallFinalMethod() {
PowerMockito.mockStatic(ClassDependency.class);
PowerMockito.when(ClassDependency.isAlive()).thenReturn(true);
FlySunDemo demo = new FlySunDemo();
Assert.assertTrue(demo.callStaticMethod());
}
}
注:当需要mock静态方法的时候,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是静态方法所在的类。
Mock 私有方法
// 测试代码
public class FlySunDemo {
public boolean callPrivateMethod() {
return isAlive();
}
private boolean isAlive() {
return false;
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest(FlySunDemo.class)
public class FlySunMockTest {
@Test
public void testCallFinalMethod() throws Exception {
FlySunDemo demo = PowerMockito.mock(FlySunDemo.class);
PowerMockito.when(demo.callPrivateMethod()).thenCallRealMethod();
PowerMockito.when(demo, “isAlive”).thenReturn(true);
Assert.assertTrue(demo.callPrivateMethod());
}
}
注:注解里写的类是私有方法所在的类。
Mock系统类的静态和final方法 :
// 测试代码
public class FlySunDemo {
public String callSystemStaticMethod(String str) {
return System.getProperty(str);
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest(FlySunDemo.class)
public class FlySunMockTest {
@Test
public void testCallSystemStaticMethod(){
FlySunDemo demo = new FlySunDemo();
PowerMockito.mockStatic(System.class);
PowerMockito.when(System.getProperty(“aaa”)).thenReturn(“bbb”);
Assert.assertEquals(“bbb”, demo.callSystemStaticMethod(“aaa”));
}
}
注:和Mock普通对象的静态方法、final方法一样