Use AbstractTransactionalSpringContextTests to rollback NUnit test case automatically

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

   1: public class BaseSpringTest : AbstractTransactionalDbProviderSpringContextTests 
   2:     {
   3:         static protected string ctxName = "application_context.xml";
   4:         static protected string[] configlocation = new string[] { "assembly://test/test.config/" + ctxName}; 
   5:  
   6:         protected override string[] ConfigLocations
   7:         {
   8:             get
   9:             {
  10:                 return configlocation;
  11:             }
  12:         }        
  13:     }
  14: public class UserTest : BaseSpringTest
  15:     {
  16:      //test case ...
  17:     }
  18: public class CustomerTest : BaseSpringTest
  19:     {
  20:      //test case ...
  21:     } 
  22:  

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.
----> System.ArgumentException : 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 the nunit run 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, hope they can fix it, so I can use my clear version of the BaseSpringService class

http://opensource.atlassian.com/projects/spring/browse/SPRNET-690

转载于:https://www.cnblogs.com/taotao/archive/2007/08/20/862840.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值