这是Spock系列的第八篇文章,上一篇介绍了Spock如何使用power mock测试静态方法,这篇讲解Spock自带的mock功能如何和power mock深度结合,发挥更强大的作用
动态mock静态方法 (spock where + power mock)
在上一篇的例子中使用power mock让静态方法返回一个指定的值,那能不能每次返回不同的值呢?
我们先看下什么场景需要这样做:
/**
* 静态方法多分支场景
* @param userVO
* @return
*/
public ListgetUserOrdersBySource(UserVO userVO){
List orderList = new ArrayList<>();
OrderVO order = new OrderVO();if ("APP".equals(HttpContextUtils.getCurrentSource())) { // 手机来源if("CNY".equals(HttpContextUtils.getCurrentCurrency())){ // 人民币// TODO 针对App端的订单,并且请求币种为人民币的业务逻辑...
System.out.println("source -> APP, currency -> CNY");
} else {
System.out.println("source -> APP, currency -> !CNY");
}
order.setType(1);
} else if ("WAP".equals(HttpContextUtils.getCurrentSource())) { // H5来源// TODO 针对H5端的业务逻辑...
System.out.println("source -> WAP");
order.setType(2);
} else if ("ONLINE".equals(HttpContextUtils.getCurrentSource())) { // PC来源// TODO 针对PC端的业务逻辑...
System.out.println("source -> ONLINE");
order.setType(3);
}
orderList.add(order);return orderList;
}
这段代码的 if else