@NotTransactional alternatives

I am working on a unit test that passes information through a DAO to an Oracle database and then extracts it and verifies that everything has not changed.

There are other unit tests in the test class, and at the top of the class:

@TransactionConfiguration (defaultRollback = true) 

I would like to know how I can remove @NotTransactional. I do not specify Transactional in the class, so by default such tests should not be like that. Since this is his own test, I do not know if the correct @BeforeTransaction (or After) annotations will be.

the biggest problem is that without @NotTransactional it seems that unsubscribe () function is not running. (basket flag does not change.)

Restarting the test with @rollback = false and @NonTransactional, I see that the trash flag is correctly set to true in the database after the test is completed.

 @RunWith (SpringJUnit4ClassRunner.class) @TransactionConfiguration (defaultRollback = true) public class DatabaseTest { @Autowired (required = true) private FooDao<Foo> fooDao; @Autowired (required = true) private FooService fooService; // other tests here that are marked @Transactional. @Test @NotTransactional public void testDelete() { Foo foo = new foo(); foo.setTrashFlag(false); foo.setId(123); fooDao.create(foo); Foo fooFromDb = fooService.getFromDbById(123); assertNotNull(fooFromDb); fooService.unsubscribe(123); // among other things, // **sets trash flag to true.** // sqlSession.flushStatements(); // doesn't seem to commit the unsubscribe action // getFromDbById() returns ONLY Foos with trash set to false, so we expect // nothing returned here as we've just set trash to true, using unsubscribe(). Foo trashedFoo = fooService.getFromDbById(123); assertNull(trashedFoo); 

Thanks!

+4
source share
2 answers

@Rollback(false) not always enough, this means that the transaction will not be rolled back at the end of the test. But it is still testing the transaction, which sometimes leads to conflicts with other running transactions.

As shown in Spring, you have two options:

  • Divide the test class into two, transactional tests can be in the test class annotated with @Transactional , and not a transactional test in the test class without transaction annotation.
  • Use method annotations instead of class annotations, annotate each transaction test with @Transactional , but remove it from the scope of the class

Quoting Spring documentation :

As with Spring 3.0, @NotTransactional deprecated in favor of moving the transaction-free method for a separate (non-transactional) test class or the @BeforeTransaction or @AfterTransaction . As an alternative to annotating an entire class with @Transactional , consider annotating individual methods using @Transactional ; to do this allows you to mix transactional and non-transactional methods into the same test class without using @NotTransactional .

+3
source

I don't know if this is really the topic, but I used @NotTransactional for methods that have a base transaction class. Spring is talking about splitting tests into transactional and non-transactional, but I found that in my case, just adding the following to the method of only disconnected transactions and let me remove the deprecated annotation:

 @Transactional(propagation = Propagation.NEVER) 

I thought I just put it for people who run in the same way as me.

+7
source

All Articles