【问题标题】:How to stop rollback in TestNG?如何在 TestNG 中停止回滚?
【发布时间】:2012-11-01 08:43:52
【问题描述】:

我有一个这样的测试:

@ContextConfiguration(locations = { "file:war/WEB-INF/application-context.xml" })
public class ServiceImplTest extends AbstractTestNGSpringContextTests
{
    @Autowired
    private Service service;

    @Rollback(false)
    @Test
    public void testCreate()
    {
           .....
              //save an entity to table_A
      service.save(a);
    }
}

table_A 将被清理;如何停止此清洁动作?

【问题讨论】:

    标签: java spring testng rollback


    【解决方案1】:

    尝试将您的测试类标记为事务性,这将要求您也使用 springs 测试类。

    @RunWith(SpringJUnit4ClassRunner.class),
    @ContextConfiguration(locations = { "file:war/WEB-INF/application-context.xml" })
    @Transactional
    public class ServiceImplTest extends AbstractTestNGSpringContextTests
    {
        @Autowired
        private Service service;
    
        @Test
        @Rollback(false)
        public void testCreate()
        {
               .....
                  //save an entity to table_A
          service.save(a);
        }
    }
    

    您也可以尝试使用@TransactionConfiguration,其中 txMgr 是您的事务管理器的名称。:

    @RunWith(SpringJUnit4ClassRunner.class),
    @ContextConfiguration(locations = { "file:war/WEB-INF/application-context.xml" })
    @TransactionConfiguration(transactionManager="txMgr", defaultRollback=false)
    public class ServiceImplTest extends AbstractTestNGSpringContextTests
    {
      ....
    }
    

    测试中的回滚功能取决于您管理事务的方式,没有这些信息很难给您一个可靠的答案。

    【讨论】:

    • 好像不是回滚而是先清理,因为每次测试后,表中所有旧数据条目都消失了,只剩下测试插入的条目。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多