我想在开始任何测试方法之前启动数据库事务,并在运行所有测试结束时回滚所有事务。
怎么做?我应该使用什么注释?
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/testApplicationContext.xml"})
public class MyTests{
public void setUp(){
//Insert temporary data to Database
}
@Test
public void testOne(){
//Do some DB transactions
}
@Test void testTwo(){
//Do some more DB transactions
}
public void tearDown(){
//Need to rollback all transactions
}
}
在春季只需添加@Transactional
对您的测试用例类进行注释:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/testApplicationContext.xml"})
@Transactional //CRUCIAL!
public class MyTests{
查看官方文档 http://static.springsource.org/spring/docs/current/spring-framework-reference/html/testing.html以获得非常深入的细节,包括@TransactionConfiguration
, @BeforeTransaction
, @AfterTransaction
和其他功能。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)