Use AbstractTransactionalSpringContextTests to rollback NUnit test case automatically

I have two test cases: UserTest and CustomerTest, both of them extend the AbstractTransactionalSpringContextTests class and using the same config file, so I wrote a BaseSpringTest class:

public class BaseSpringTest : AbstractTransactionalDbProviderSpringContextTests
{
    static protected string ctxName="application_context.xml";
    static protected string[] configlocation = new string[]{"assembly://test/test.config/" + ctxName};

    protected override string[]ConfigLocations
    {
        get
        {
            return configlocation;
        }
    }
}

public class UserTest : BaseSpringTest
{
    //test case ...
}

public class CustomerTest : BaseSpringTest
{
    //test case ...
}

When I run the tests, the first one runs ok, but the second one throws an exception:

Spring.Objects.Factory.ObjectCreationException : 
        Error creating object with name 'xxxService' defined in 
        'assembly [Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null], resource [test.config.Service.xml]' :
        Initialization of object failed : Duplicate type name within an assembly.

I traced into AbstractSpringContextTests class and found that it’s using a hashtable to cache the loaded context, but the hashtable isn’t a static field:

private IDictionary contextKeyToContextMap = new Hashtable();

So the problem is it load the context and put to the hashtable, but the hashtable is only available in the same class,
when nunit runs the second test, the hashtable is empty again,
so it try to load the context again, then the exception occurs.
So I think just change the hashtable to static is ok. but since spring.net is still in 1.1 RC1, we have to use another way temporarily.

What I’m doing now is cached the context again in the baseSpringTest class:

public class BaseSpringTest : AbstractTransactionalDbProviderSpringContextTests
{
    static protected string ctxName = "application_context.xml";
    static protected string[] configlocation = new string[] { "assembly://Test.Service/Test.config/" + ctxName};
    static protected IConfigurableApplicationContext cachedApplicationContext = null;
    
    protected override string[] ConfigLocations
    {
        get{ return configlocation; }
    }
    
    [SetUp]
    public new void SetUp()
    {
        if (cachedApplicationContext == null) {
            XmlConfigurator.Configure();
            base.SetUp();
            cachedApplicationContext = applicationContext;
        }
        
        applicationContext = cachedApplicationContext;
        
        //you have to call the following, else the transaction won't rollback.
        EndTransaction();
        InjectDependencies();
        
        try {
            OnSetUp();
        } catch (Exception ex) {
            logger.Error("Setup error", ex);
            throw;
        }
    }
}

Spring.net is a great work, but why this bug keeps so long time, and nobody submit it?
So I submitted the bug, http://opensource.atlassian.com/projects/spring/browse/SPRNET-690
hope they can fix it, so I can use my BaseSpringService class for shared behaviours across tests.

UPDATE on 2007-08-21: The spring.net team had fixed the problem

Leave a Reply

Your email address will not be published. Required fields are marked *