在使用JUnit进行DAO的单元测试的时候
显示错误:
org.hibernate.TransactionException: Transaction not successfully started
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:100)
at test.TestUserDao.tearDown(TestUserDao.java:28)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
以下是测试类TestUserDao:
package test;
import dao.DaoFactory;
import dao.IUserDAO;
//import dao.UserDAO;
import exception.BlogDAOException;
import junit.framework.TestCase;
import hibernate.HibernateUtil;
import hibernate.User;
public class TestUserDao extends TestCase {
IUserDAO userDAO;
static String username = "test " + (int)(Math.random() * 100000);
static String userid;
protected void setup() throws Exception
{
super.setUp();
userDAO = (IUserDAO)DaoFactory.getDao( "userDao ");
assertNotNull( "User DAO is NULL! ", userDAO);
HibernateUtil.getSessionFactory().openSession().beginTransaction();
}
protected void tearDown() throws Exception
{
super.tearDown();
HibernateUtil.getSessionFactory().openSession().getTransaction().commit();
}
//测试saveUser()方法
public void testSaveUser()
{
User user = new User();
user.setUsername(username);
user.setPassword( "test ");
userDAO.saveUser(user);
assertNotNull( "Save User Sucess! ", user.getId());
userid = user.getId();
//测试同名保存
User u3 = new User();
u3.setUsername(username);
u3.setPassword( "ttt ");
boolean result = false;
try
{
userDAO.saveUser(u3);
}
catch (BlogDAOException ex)
{
result = true;
}
assertTrue( "The user Not Exists! ", result);
}
//测试getUser()方法
public void testGetUser()
{
User user = userDAO.getUser(username);
assertNotNull( "Get User Success! ", user);
}
//测试getUserById()方法
public void testGetUserById()
{
User u = userDAO.getUserById(userid);
assertNotNull( "Get (By Id) User Sucess! ", u);
}
}
------解决方法--------------------------------------------------------
事务的控制没有提交...保存完调用下commit();
------解决方法--------------------------------------------------------
你不应该调用openSession,因为这样是打开了一个新的session。
你应该每次操作openSession并一直持有它,在操作完成后才commit和close
------解决方法--------------------------------------------------------
在tearDown()里提交事务了啊~
======================================
save()后了你好象没有调用tearDown()吧!还是事务没有提交啊!debug下就清楚了!
------解决方法--------------------------------------------------------