首页 > 学院 > 开发设计 > 正文

SpringTest 使用说明 -构建无污染纯绿色事务测试框架 (记录用)

2019-11-15 00:57:27
字体:
来源:转载
供稿:网友
SPRingTest 使用说明 -构建无污染纯绿色事务测试框架 (记录用)

@ContextConfiguration({"classpath:applicationContext.xml","classpath:spring/buyer/applicationContext-service.xml"})导入配置文件,这里我的applicationContext配置文件是根据模块来分类的。如果有多个模块就引入多个“applicationContext-service.xml”文件。如果所有的都是写在“applicationContext。xml”中则这样导入: @ContextConfiguration(locations = "classpath:applicationContext.xml")@RunWith(SpringJUnit4ClassRunner.class) SpringJUnit支持,由此引入Spring-Test框架支持!@Transactional 这个非常关键,如果不加入这个注解配置,事务控制就会完全失效!

@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)这里的事务关联到配置文件中的事务控制器(transactionManager = "transactionManager"),同时指定自动回滚(defaultRollback = true)。这样做操作的数据才不会污染数据库

@RunWith(SpringJUnit4ClassRunner.class)@Transactional@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)@ContextConfiguration(locations = "classpath:applicationContext.xml")public class NodeServiceTest extends AbstractJUnit4SpringContextTests {//balabala}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表