@Rollback (false) does not work in @ Before using SpringJUnit4ClassRunner

In a JUnit test in my Spring application, I would like to insert a lot of data into the installation method, and then use it for testing. However, what was done in the method @Beforeseems to be rolled back after each test, even if I annotate the method with@Rollback(false)

Here is 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 also tried using @BeforeClass, but this requires the method to be static and execute before the setter methods are called @Autowired, so I cannot access the services that I need to call when it @BeforeClassis executed.

@PostConstruct, ( , Hibernate ). , , , , , , , , , , , . @BeforeTransaction, , .

+5
1

TransactionalTestExecutionListener Junit. (beforeTestMethod afterTestMethod), Junit.

@Before , , , . @Rollback, Test, setUp @Before

, , : ( 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);
        }
    }
+2

All Articles