2012-08-05 78 views
6

我作为单元测试运行,即使在春季3.1中不使用@rollback,它也会自动返回。 我的测试看起来像@Transactional是否会在Spring 3中自动回滚事务?

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "classpath:application-context.xml" }) 
public class PersonServiceTest { 

    @Test 
    @Transactional 
    public void savePerson() { 
      Person person = createPerson(); 
      personService.savePerson(person); 
    } 
} 

回滚行为是否默认设置?

+0

TL; DR:是的,这是默认设置。 – 2015-08-15 03:21:35

回答

13

默认SpringJUnit4ClassRunner会自动回滚事务。

为了消除影响,请在您的测试课程上使用@TransactionConfiguration(defaultRollback=false)或在每个测试中使用@Rollback(false)

+0

...在Spring 5上,'@ Commit'代替'@Rollback(false)' – 2018-03-08 19:04:22