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: Continue reading “Use AbstractTransactionalSpringContextTests to rollback NUnit test case automatically”