These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: CI, Unit Testing and NHibernate
PostPosted: Tue Nov 06, 2007 5:39 am 
Regular
Regular

Joined: Fri May 05, 2006 11:54 am
Posts: 51
Hi,
I am trying to set up a continuous integration server that not surprisingly has a collection of Unit tests that uses the NUnit framework.

All my tests run fine when I run them individually but when I run the whole testsuite I run into into session problems.

I am getting some errors such as:

Quote:
object references an unsaved transient instance - save the transient instance before flushing:


Or a very strange one happens in the following circumstances:

UnitTest1 is testing for an expected exception and does not commit a saveorupdate.

UnitTest2 is doing a Criteria.List and I get an exception telling me it could not insert the data from UnitTest1.

I get the following error

Quote:
tests.people.PeopleControllerTest.SiteGetAll : could not insert: [continuity2.core.domain.Site#d0842278-3d8e-429a-8efc-9863fa9e2501][SQL: INSERT INTO Sites (name, deleted, companyuid, businessunituid, ssid, Uid) VALUES (?, ?, ?, ?, ?, ?)]


I have tried adding the following StartUp and TearDown procedures but I am getting nowhere fast.

Code:
[SetUp]
public void Init()
{
    SetUserToSystemAdmin();
    _peopleController = _container.Resolve<PeopleController>();
    _activeCompany = GetFirstItem<Company>();
    Assert.IsNotNull(_activeCompany);
    _context = _container.Resolve<IContext>();
    _session = _sessionFactory.OpenSession();
    _contactController = _container.Resolve<ContactController>();
}

[TearDown]
public void Dispose()
{
    try
    {
        Company company = _commonService.GetObjectByDescription<Company>("Name", COMPANY_NAME);
        if(company != null)
            _peopleController.DeleteCompany(company.Uid);
        if(_session != null)
        {
            _session.Flush();
            _session.Clear();
            _session.Dispose();
        }
    }
    catch
    {
        //do nothing
    }
}


I am using the Caslte.Services.Transaction transaction attributes my NHibernate Crud operations.

Can anyone help me out as to WTF is going on here?

Thanks

Paul


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 06, 2007 1:59 pm 
Beginner
Beginner

Joined: Wed Oct 04, 2006 8:57 am
Posts: 25
It sounds like you are storing a reference to an object at the class level and using it from within another test. If you are doing that then you will need to keep your session around for the life of the test fixture. I would recommend scoping it to the individual test.

I would recommend using TestFixtureBase base from Rhino.Commons.
It makes testing really easy.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 06, 2007 3:19 pm 
Beginner
Beginner

Joined: Mon Aug 15, 2005 11:38 am
Posts: 28
One thing I noticed is that your Dispose method will never execute Session.Clear and Session.Dispose if the Flush fails. So maybe your clean-up code is flawed.

Christoph


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 07, 2007 3:59 am 
Regular
Regular

Joined: Fri May 05, 2006 11:54 am
Posts: 51
Neither to both of the above.

The clean up code always runs but _session.Clear() does not appear to be doing anything.

I am veryconfused.

It is almost like the session I am getting a reference to is not the correct session.

It seems the previous test case is not cleaning up the session and I do not know why.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 07, 2007 12:57 pm 
Newbie

Joined: Thu Oct 20, 2005 5:17 pm
Posts: 19
I agree with Derek and also recommend scoping to the individual test.

Why you should not use SetUp and TearDown in NUnit.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 08, 2007 9:21 am 
Regular
Regular

Joined: Fri May 05, 2006 11:54 am
Posts: 51
I scoped the session at the method level and it went away.

Thank you for your help on this one.

Interesting point about SetUp and TearDown


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.