PowerMock单元测试案例

引入如下的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方法一样

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值