Hello,
Using JUnit 3.x and org.springframework.test.AbstractTransactionalSpri ngContextTests in Spring 2.5, I was able to start and stop transactions when I wanted. As it is explained in Spring 2.5 reference documentation for transaction management in Unit test (http://static.springsource.org/sprin...it38-legacy-tx), I just called methods endTransaction(..) and startNewTransaction(..).
Now, I would like to do the same thing with Spring 3.0 and JUnit 4.8.1. I read the reference documentation for unit testing (http://static.springsource.org/sprin...l/testing.html) but I did not find anything to do the same thing. As far as I have understood, the transaction management in Unit tests is now done via @Transactional (which i already use for my service layer in Spring 2.5).
I extend my JUnit test class with AbstractTransactionalJUnit4SpringContextTests. This class is already annoted with @Transactional at class level. So all the test method are run in a transaction which rollback by default. However, how to stop and start new transaction ? I tried the code below but it does not insert rows as I expected :
All the rows are inserted at the same time at the end of the JUnit. It seems that the transaction which applies is the global one of the test() method and not the new one I would like in transInsert(..) method.Code:@ContextConfiguration(locations = { "..." }) public class TransactionTest extends AbstractTransactionalJUnit4SpringContextTests { @Test @Rollback(false) public void test() { String req = "select num from table1"; List<Integer> nums = jdbcTemplate.queryForList(req, Integer.class); for (Integer i : nums) { this.transInsert(i); } } // I would like a new transaction and a commit @Transactional(propagation = Propagation.REQUIRES_NEW) @Rollback(false) public void transInsert(int i) { String req = "insert into table2 values (?)"; jdbcTemplate.update(req, i); } }
So, how can I stop and start new transactions like I did in Spring 2.5 with endTransaction(..) and startNewTransaction(..) ?
Thanks in advance for your help.


Reply With Quote