public class DrawDao extends HibernateDaoSupport {
/**
* 添加领用申请
*
* @param draw
* 领用申请信息(不可为null)
* @return
*/
@SuppressWarnings("unchecked")
public boolean insertDraw(Carddrawapply draw, List<Carddrawapplydetail> drawdetailList) {
boolean flag = false;
Session session = null;
Transaction tx = null;
try {
session = this.getSession();
tx = session.beginTransaction();
long shopid = MyConstant.HTDZ_DEFAULT_SHOPID;//商家ID
String billtype = CardConstant.CARD_LY_ID;//业务编号
String engshort = MyConstant.HTDZ_STR;//商家英文简称
String busishort = CardConstant.CARD_LY_CODE;//业务英文简称
String formcode = CardCode.getFormCode(session,shopid,billtype,engshort,busishort);
if(!"".equals(formcode)){
draw.setCarddrawapplycode(formcode);
draw.setSourcetype("0");
draw.setApplydate(MyFormat.getToday());
draw.setStatus(StatusUtil.CARDDRAWAPPLY_STATUS01);
session.save(draw);
long carddrawapplyid = draw.getCarddrawapplyid();
session.flush();
if(drawdetailList.size()==0){
tx.rollback();
}else{
for(Carddrawapplydetail drawdetail : drawdetailList){
if(drawdetail!=null){
drawdetail.setCarddrawapplyid(carddrawapplyid);
session.save(drawdetail);
}
}
tx.commit();
flag = true;
}
}
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
flag = false;
} finally {
session.close();
}
return flag;
}
}
以上Dao的方法已经正常。
对此方法的测试,我是在后期补上的。但是居然报错。帮我看看:
Junit4:(不用继承TestCase)
public class TestDrawDao{
private DrawDao drawDao;
@Before
public void setUp(){
drawDao = new DrawDao();
}
/**
* 测试'卡领用申请信息的添加'
*/
@Test
public void testInsertDraw(){
Carddrawapply draw = new Carddrawapply();
draw.setApplyoperid(1);
draw.setApplydesc("申请说明");
List<Carddrawapplydetail> drawdetailList = new ArrayList<Carddrawapplydetail>();
//IC卡,100元面值的100张
Carddrawapplydetail drawdetail1 = new Carddrawapplydetail();
drawdetail1.setCardtype("1");
drawdetail1.setParvalue(100);
drawdetail1.setAmount(100);
//充值卡,50元面值的50张
Carddrawapplydetail drawdetail2 = new Carddrawapplydetail();
drawdetail2.setCardtype("2");
drawdetail2.setParvalue(50);
drawdetail2.setAmount(50);
drawdetailList.add(drawdetail1);
drawdetailList.add(drawdetail2);
boolean flag = drawDao.insertDraw(draw, drawdetailList);
//断言
Assert.assertEquals(true,flag);
}
}
异常如下:
java.lang.NullPointerException
at org.springframework.orm.hibernate3.support.HibernateDaoSupport.getSession(HibernateDaoSupport.java:142)
at com.htdz.card.admin.dao.DrawDao.insertDraw(DrawDao.java:35)
也就是调用到DrawDao里insertDraw()的这句时报错:session = this.getSession();
业务类里调用这个Dao都能正常使用,为什么从测试类里调用就报这个错误?
DrawDao继承了HibernateDaoSupport了啊。