junit4 的使用 顺便理解ClassPathXmlApplicationContext的使用

工作中,需要给同事在dao层写个方法,写完后,只能用junit测试,如是学习了junit4的使用。

先用eclipse引入junit4相关包,然后写个类如下,就行了。

public class Test extends TestCase{
	
	private IDiscountDao discountDao;

	@Override
	protected void setUp() throws Exception {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		discountDao =context.getBean(IDiscountDao.class);
	}
	
	public void test1(){
		DiscountInfo info = discountDao.getDiscForZhuanban("036596782c9611e2b12d00215e6e7653");
		System.out.println(info);
	}
}

 框架用的是springmvc + ibatis ,各种类都是注解的。

discountDao =context.getBean(IDiscountDao.class);这样取到要测试的dao类。

ClassPathXmlApplicationContext("applicationContext.xml")这个表示取类路劲下的applicationContext.xml,即web-info/classes/下的applicationContext.xml文件,
也就是源码中src下面的applicationContext.xml文件。
原文地址:https://www.cnblogs.com/xiongjinpeng/p/2854103.html