第一章 PowerMock单元模拟测试框架简介
第二章 PowerMock模拟功能异常
在实际工作中是否碰到,当某些方法或属性定为私有的,是比较重要的功能点需要用单元测试验证。在不改变代码的情况下,如何验证?对了,使用PowerMock轻松解决。PowerMock的诞生就为了解决其他单元测试框架不能解决的问题。工作中多做本地单元测试,可以有效提前发现问题并及时处理,最后做到每个功能"0"bug,代码的质量也有了保障。这段时间连续发了几遍关于PowerMock文章,是个人学习的总结分享,而学PowerMock的原因,是在一次读spring源码时发现spring每一个模块必定会有单元测试类(如下图),经典框架都怎么做了,所以也跟着学了。 下面分享测试方法。
spring-beans源码单元测试案例示意图
一 PowerMock测试类普通私有方法和静态私有方法。
1 PowerMock功能调用
PowerMock.when(Object instance, String methodName, Object... arguments)
PowerMockito.verifyPrivate(Object instance).invoke(, String methodName, Object... arguments);
2 被测试普通私有方法和静态私有方法demo
package powerMock;
public class OrderService {
private OrderDao orderDao;
public void setOrderDao(OrderDao orderDao) {
this.orderDao = orderDao;
}
public OrderService(OrderDao orderDao) {
this.orderDao = orderDao;
}
//普通私有方法
private int update1(){
try{
orderDao.update();
}catch (Exception e){
e.printStackTrace();
}
return 1;
}
//静态私有方法
private static void updateById(){
System.out.println("updateById");
}
//静态带参私有方法
private static void updateById(String param){
System.out.println("updateById:" +param);
}
//测试静态带参数和返回值的方法
private static int updateByIdWithResult(String param){
System.out.println("updateByIdWithResult:" +param);
return 0;
}
}
3 测试普通私有方法和静态私有方法demo
package powerMock;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({StringUtils.class})
public class TestOrderService {
//测试普通私有方法
@Test
public void testPrivateMethod(){
OrderDao orderDao=PowerMockito.mock(OrderDao.class);
OrderService orderService=new OrderService(orderDao);
OrderService spy=PowerMockito.spy(orderService);
try {
PowerMockito.when(spy, "update1",new Object[]{});
} catch (Exception e) {
e.printStackTrace();
}
}
//测试普通私有方法另一种方法
@Test
public void testPrivateMethod1(){
OrderDao orderDao=new OrderDao();
OrderService orderService=new OrderService(orderDao);
try {
PowerMockito.verifyPrivate(orderService).invoke("update1",new Object[]{});
} catch (Exception e) {
e.printStackTrace();
}
}
//测试静态私有方法
@Test
public void testPrivateStaticMethod(){
OrderDao orderDao=new OrderDao();
OrderService orderService=new OrderService(orderDao);
try {
PowerMockito.verifyPrivate(orderService).invoke("updateById",new Object[]{});
} catch (Exception e) {
e.printStackTrace();
}
}
//测试静态私有方法带参数
@Test
public void testPrivateStaticMethodWithParameter(){
OrderDao orderDao=new OrderDao();
OrderService orderService=new OrderService(orderDao);
try {
PowerMockito.verifyPrivate(orderService).invoke("updateById",new Object[]{"1"});
} catch (Exception e) {
e.printStackTrace();
}
}
//测试静态私有方法带参数
@Test
public void testPrivateStaticMethodWithParameterResult(){
OrderDao orderDao=new OrderDao();
OrderService orderService=new OrderService(orderDao);
try {
PowerMockito.verifyPrivate(orderService).invoke("updateByIdWithResult",new Object[]{"1"});
} catch (Exception e) {
e.printStackTrace();
}
}
}
二 PowerMock测试类普通私有成员属性和静态私有成员属性
1 PowerMock功能调用
PowerMockito.field(Class<?> declaringClass, String fieldName).set(Object obj, Object value)
2 被测试普通私有成员属性和静态私有成员属性demo
package powerMock;
public class OrderService {
private OrderDao orderDao;
private Integer param;
private static Integer result;
public void setOrderDao(OrderDao orderDao) {
this.orderDao = orderDao;
}
public OrderService(OrderDao orderDao) {
this.orderDao = orderDao;
}
public void showPrivateFild(){
System.out.println("showPrivateFild:"+param);
}
public static void showPrivateStaticFild(){
System.out.println("showPrivateStaticFild:"+result);
}
}
3 普通私有成员属性和静态私有成员属性demo
package powerMock;
import com.fast.common.core.utils.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.junit.Assert.assertEquals;
@RunWith(PowerMockRunner.class)
@PrepareForTest({StringUtils.class})
public class TestOrderService {
//测试普通私有成员属性
@Test
public void testShowPrivateFild(){
OrderDao orderDao=PowerMockito.mock(OrderDao.class);
OrderService orderService=new OrderService(orderDao);
try {
PowerMockito.field(OrderService.class,"param").set(orderService,2);
orderService.showPrivateFild();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
//测试普通私有静态成员属性跟上面的一样
@Test
public void testShowPrivateStaticFild(){
OrderDao orderDao=PowerMockito.mock(OrderDao.class);
OrderService orderService=new OrderService(orderDao);
try {
PowerMockito.field(OrderService.class,"result").set(orderService,2);
orderService.showPrivateStaticFild();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
|