Results 1 to 2 of 2

Thread: A problem with transaction.

  1. #1
    Join Date
    May 2007
    Location
    Foshan Guangdong China
    Posts
    12

    Question A problem with transaction.

    I want to do these in my vs2005 test class:
    Code:
            [TestInitialize()]
            public void MyTestInitialize()
            {
                //begin a transaction
                //.....
            }
    
            [TestCleanup()]
            public void MyTestCleanup()
            {
                //rollback...
                //because I don't want the test data update into database.
            }
    
            [TestMethod()]
            public void DAOTestMethod()
            {
                //Use dao to do some database operations.
                //In dao, I User HibernateTemplate to access database.
            }
    My question is how to code in MyTestInitialize() and MyTestCleanup() for begin and rollback the transaction.

    I try these:
    Code:
            HibernateTemplate ht = null;
    
            public void DAOTestMethod()
            {
                this.ht.Execute<SnapShot>(new HibernateDelegate<SnapShot>(
                        delegate(ISession session)
                        {
                            this.DAOTestMethodTaget();
                            session.Transaction.Rollback();//the transaction is commited. so this code would throw an exception.
                            return null;
                        }
                    )
                );
            }
    
            public void DAOTestMethodTaget()
            {
                //....
                TestDAO.Save(myTestObj);
                //....
            }
    but is doesn't work.

    BTW: Any better suggestions for Create Test Case? I just want to test the dao without updating data into database.

  2. #2
    Join Date
    May 2006
    Posts
    36

    Default

    check this out:

    http://www.springframework.net/doc-l...tml#testing-tx

    It will do exactly what you want.

    -Shane

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •