工作中,需要給同事在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文件。
