junit.framework.AssertionFailedError: expected:<true> but was:<false> at junit.framework.Assert.fail(Assert.java:57) at junit.framework.Assert.failNotEquals(Assert.java:329) at junit.framework.Assert.assertEquals(Assert.java:78) at junit.framework.Ass
package service.impl; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import db.MyHibernateSessionFactory; import entity.Users; import service.UsersDAO; public class UsersDAOImpl implements UsersDAO { @Override public boolean usersLogin(Users u) { // 事物对象 Transaction tx = null; String hql = ""; try { Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession(); tx = session.beginTransaction(); hql = "from Users where username=? and password=? "; Query query = session.createQuery(hql); query.setParameter(0, u.getUsername()); query.setParameter(1, u.getPassword()); List<?> list = query.list(); tx.commit();//提交事物 if (list.size()>0) { return true; } else { return false; } } catch (Exception ex) { ex.printStackTrace(); return false; } finally { if (tx != null) { tx = null; } } } }
package service.impl; import org.junit.Test; import entity.Users; import junit.framework.Assert; import service.UsersDAO; public class TestUsersDAOImpl { @Test public void testUsersLogin() { Users u = new Users(1, "zhangsan", "123456"); UsersDAO udao = new UsersDAOImpl(); // udao.usersLogin(u); Assert.assertEquals(true, udao.usersLogin(u)); //将expected改为false则测试通过 } }
DeBug显示返回的list的 size 0