Spock怎么测试Service中的Mapper

之前在博客园上提的一个问题,没有人答复,自己解决了:
在这里插入图片描述

把赋值的语句写到given语句块,如:

given :

def backBill_rel = new BackBill(billNo : "002") 
def list = new ArrayList<BackBill>()
list.add( backBill_rel )       
backBillMapper.selectList( _ as QueryWrapper) >> list

高级用法

对静态对象赋值,比如applicationContext无法自动注入时

@Component
public class ApplicationContextUtils implements ApplicationContextAware {

	private static ApplicationContext applicationContext = null;

	/**
	 * 获取静态变量中的ApplicationContext.
	 */

	public static ApplicationContext getApplicationContext() {

		return applicationContext;

	}

	/**
	 * 从静态变量applicationContext中得到Bean, 自动转型为所赋值对象的类型.
	 */

	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name) {

		return (T) applicationContext.getBean(name);

	}

	/**
	 * 从静态变量applicationContext中得到Bean, 自动转型为所赋值对象的类型.
	 */
	public static <T> T getBean(Class<T> requiredType) {

		return applicationContext.getBean(requiredType);
	}

	/**
	 * 清除SpringContextHolder中的ApplicationContext为Null.
	 */
	public static void clearHolder() {

		applicationContext = null;

	}

	/**
	 * 实现ApplicationContextAware接口, 注入Context到静态变量中.
	 */

	@Override
	public synchronized void setApplicationContext(ApplicationContext applicationContext) {

		ApplicationContextUtils.applicationContext = applicationContext;

	}

}

Spock写法:

given:
ApplicationContextUtils.applicationContext = Mock(ApplicationContext)
ApplicationContextUtils.applicationContext.getBean( _ ) >> Mock(BusinessTransactionRunner)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值