只需
@Transactional在测试之上添加注释:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"testContext.xml"})@Transactionalpublic class StudentSystemTest {默认情况下,Spring将启动围绕你的测试方法和
@Before/ @After回调的新事务,并在最后回滚。默认情况下它可以工作,在上下文中有一些事务管理器就足够了。
来自:10.3.5.4事务管理(粗体):
在TestContext框架中,事务由
TransactionalTestExecutionListener进行管理。请注意,即使你未在测试类上显式声明,它
TransactionalTestExecutionListener也是默认配置的
@TestExecutionListeners。但是,要启用对事务的支持,必须
PlatformTransactionManager在由
@ContextConfiguration语义加载的应用程序上下文中提供一个bean 。此外,你必须
@Transactional在类或方法级别为测试声明。



