在使用SpringJUnit4ClassRunner的

在使用SpringJUnit4ClassRunner的

本文介绍了@Rollback(false)在使用SpringJUnit4ClassRunner的@Before上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring应用程序的JUnit测试中,我想在设置方法中插入很多数据,然后使用它进行测试.但是,即使我用@Rollback(false)

注释了该方法,在每次测试后,在@Before方法中执行的任何操作都似乎会回滚.

这是我要做的事情的简化版本:

public class TestClass
{
   @Autowired
   MyService service;

   @Before
   public void setup()
   {
      if(service.getById(1) == null)
      {
         Thing thing = new Thing();
         thing.setId(1);
         service.create(new Thing(1))
      }
   }
}

我也尝试过使用@BeforeClass,但是这要求该方法是静态的,并且必须在调用任何@Autowired setter方法之前执行,所以当@BeforeClass运行.

我尝试使用@PostConstruct,但是存在可用事务存在的问题(我的设置是仅在事务开始时才能使用Hibernate会话).奇怪的是,一个会话似乎可用,但是从同一会话中获得的两个对象不相等,这意味着Hibernate 1级缓存似乎失败了,或者每个操作都在一个单独的会话中进行. @BeforeTransaction似乎表现出相同的行为.

解决方案

The Spring TransactionalTestExecutionListener 负责管理Junit测试的事务.它使用两种方法( beforeTestMethod afterTestMethod )来开始和结束每个Junit测试的事务.

对于@Before注释,它似乎是这样工作的,它通过@Before注释将在Test方法上指定的 @Rollback属性应用于setUp方法.

我有这个例子来解释这个过程,我有两种测试方法,一种是(回滚为false,另一种是回滚为真)

      @RunWith(SpringJUnit4ClassRunner.class)
      @ContextConfiguration(loader=AnnotationConfigContextLoader.class, classes={SpringConfig.class})
      @Transactional
       public class MyTest
      {

        @Before
        public void setUp()
        {
            //When executing this method setUp
            //The transaction will be rolled back after rollBackTrue Test
            //The transaction will not be rolled back after rollBackFalse Test
         }


        @Test
        @Rollback(true)
        public void rollBackTrue()
        {
            Assert.assertTrue(true);
        }

        @Test
        @Rollback(false)
        public void rollBackFalse()
        {
            Assert.assertTrue(true);
        }
    }

In a JUnit test in my Spring application, I'd like to insert a lot of data in a setup method, and then use it to test against. However, whatever is done in the @Before method appears to be rolled back after each test, even if I annotate the method with @Rollback(false)

Here's a simplified version of what I'm trying to do:

public class TestClass
{
   @Autowired
   MyService service;

   @Before
   public void setup()
   {
      if(service.getById(1) == null)
      {
         Thing thing = new Thing();
         thing.setId(1);
         service.create(new Thing(1))
      }
   }
}

I've also tried using @BeforeClass, but that requires the method to be static, and executes before any @Autowired setter methods are called, so I can't get access to the services I need to call when @BeforeClass runs.

I tried using @PostConstruct, but there are issues with having a transaction available (and my setup is such that a Hibernate session is only available when a transaction starts). Weirdly a session seemed to be available, but two objects got from within the same session were not equal, meaning Hibernate 1st-level cache seemed to be failing, or each operation was happening in a separate session. @BeforeTransaction seemed to exhibit the same behaviour.

解决方案

The Spring TransactionalTestExecutionListener is responsible for managing the transaction for the Junit Tests. It uses two methods (beforeTestMethod and afterTestMethod) to start and end the transaction for each of the Junit Tests.

As for @Before annotation it seems to work like this, It applies the @Rollback attribute specified on the Test method to the to setUp method with @Before annotation

I have this example to explain the process, I have two test methods one with (roll back false the other with roll back true)

      @RunWith(SpringJUnit4ClassRunner.class)
      @ContextConfiguration(loader=AnnotationConfigContextLoader.class, classes={SpringConfig.class})
      @Transactional
       public class MyTest
      {

        @Before
        public void setUp()
        {
            //When executing this method setUp
            //The transaction will be rolled back after rollBackTrue Test
            //The transaction will not be rolled back after rollBackFalse Test
         }


        @Test
        @Rollback(true)
        public void rollBackTrue()
        {
            Assert.assertTrue(true);
        }

        @Test
        @Rollback(false)
        public void rollBackFalse()
        {
            Assert.assertTrue(true);
        }
    }

这篇关于@Rollback(false)在使用SpringJUnit4ClassRunner的@Before上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 20:39