我用的是MYeclipse 為什么用junit4單元測試,可以正常執(zhí)行usersAction去調(diào)用UsersDaoImpl代碼塊,但是用struts.xml來調(diào)用UsersAction去執(zhí)行UsersDaoImpl,總是報異常
我用的是MYeclipse 為什么用junit4單元測試,可以正常執(zhí)行usersAction去調(diào)用UsersDaoImpl代碼塊,但是用struts.xml來調(diào)用UsersAction去執(zhí)行UsersDaoImpl,總是報異常,通過測試發(fā)現(xiàn) 用struts.xml來調(diào)用UsersAction去執(zhí)行UsersDaoImpl代碼塊時 try....catch....里面的語句不執(zhí)行,但是但用單元測試工具junit4去執(zhí)行UsersAction代碼去調(diào)用UsersDaoImpl代碼塊時,可以正常運行try...catch....里面的代碼!有輸出不解其中原因
2016-12-27
附上代碼,求大神指點
public boolean userLogin(User u) {
System.out.println(u);//測試值傳過來沒有
//創(chuàng)建一個事物對象
Session session = HibernateSessionFactory.getSession();
//注意:getTransaction和beginTransaction的區(qū)別
Transaction tx = session.getTransaction();
try{
tx.begin();
//此處不能省略
String hql = "from User where username=? and userpassword=?";
Query query = session.createQuery(hql);
//設置用戶名和密碼0代表where后面的第一個參數(shù),1代表where第二個參數(shù)
query.setParameter(0, u.getUsername());
query.setParameter(1, u.getUserpassword());
List<User> list = query.list();
//提交事物(只有在增刪改的時候才需要事物)
tx.commit();
// session.close();
//判斷是否查詢到了用戶
//注意:方法的返回值默認以try里面的為準,之所以還要再加一個return返回值,是為了語法不出錯而已,返回結果沒有實際意義
if(list!=null){
System.out.println("AAAAAAAAAAAAAA");
return true;
}else{
return false;
}
}catch(Exception ex){
ex.printStackTrace();
tx.commit();
}finally{
if(tx != null){
tx=null;
}
}
return false;
}
}