http://nottiansyf.iteye.com/blog/345819

使用Junit4.4测试 
在类上的配置Annotation 
@RunWith(SpringJUnit4ClassRunner.class) 用于配置spring中测试的环境 
@ContextConfiguration(locations={"classpath:spring/applicationContext.xml","classpath:spring/spring-config-*.xml"})用于指定配置文件所在的位置
@Test标注在方法前,表示其是一个测试的方法 无需在其配置文件中额外设置属性. 

多个配置文件时{"/applic","/asas"} 可以导入多个配置文件 
WEB-INF/classes/spring/beanRefContext.xml配置路径:classpath:spring/beanRefContext.xml

测试中的事务配置 , 
AbstractTransactionalJUnit38SpringContextTests、 AbstractTransactionalJUnit4SpringContextTests
AbstractTransactionalTestNGSpringContextTests 
已经在类级别预先配置了好了事物支持 

在普通spring的junit环境中配置事务 
在类之前加入注解 
@TransactionConfiguration(transactionManagert="txMgr",defaultRollback=false) 
@Transactional 
在方法中主要使用的Annotation包括 
@TestExecutionListeners({})---用于禁用默认的监听器 否着需要通过@contextconfiguration配置一个ApplicationContext;

@BeforeTransaction 
@Before 
@Rollback(true) 
@AfterTransaction 
@NotTransactional 

Junit4.4下支持类,方便基于junit4.4的测试 
AbstractJUnit4SpringContextTests: 

AbstractTransactionalJUnit4SpringContextTests: 
需要在applicationContext中定义一个datasource 

2009年3月9日 
目前Spring2.5只支持4.4的Junit进行测试 
下面是一个简单的测试Demo

[html] view plain copy
  1. package cn.com.bhgx.business.module.customerinfo.test;
  2. import javax.annotation.Resource;
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.test.context.ContextConfiguration;
  6. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  7. import org.springframework.test.context.transaction.TransactionConfiguration;
  8. import org.springframework.transaction.annotation.Transactional;
  9. import cn.com.bhgx.business.module.customerinfo.service.ICustomerService;
  10. @RunWith(SpringJUnit4ClassRunner.class)
  11. @ContextConfiguration(locations={"classpath:spring/web-application-common.xml","classpath:spring/web-application-dao.xml","classpath:spring/web-application-service.xml"})
  12. @TransactionConfiguration(transactionManager="transactionManager",defaultRollback=false)
  13. @Transactional
  14. public class TestAllFunction {
  15. @Resource(name="customerService")
  16. private ICustomerService customerService;
  17. @Test
  18. public void getAllCustomers() {
  19. customerService.getCustomerById("1241470");
  20. }
  21. }

最新文章

  1. norm函数的作用,matlab
  2. offset笔记
  3. MFC学习 socket
  4. C#传递参数
  5. Appium对京东App中WebView的处理
  6. Spring强制使用CGLIB代理事务
  7. 使用Knoctout.分页
  8. C++标准库之泛型算法
  9. CentOS minimal 版安装图形界面及中文语言
  10. Java中容器的两种初始化方式比较
  11. [转载] 红黑树(Red Black Tree)- 对于 JDK TreeMap的实现
  12. JavaScript 原型中的哲学思想
  13. Scrapy爬虫遇到 ‘Forbidden by robots.txt’的问题
  14. React列表
  15. 潭州课堂25班:Ph201805201 爬虫高级 第九课 scrapyd 部署 (课堂笔记)
  16. linux安装nodejs运行vue程序
  17. Integer to Boolean strange syntax
  18. [epoll]epoll理解
  19. 【前端系列】移动前端开发之viewport的深入理解
  20. JS 创建对象总结

热门文章

  1. hdu 5532(最长上升子序列)
  2. 例10-12 *uva1637(概率dp)
  3. 例10-11 uva11181
  4. 在QEMU中调试ARM程序【转】
  5. SpringBoot学习之集成dubbo
  6. Linux查看Tomcat是否多次重启命令
  7. Spring 自带的定时任务Scheduled
  8. 初识 Runtime
  9. webpack 前后端分离开发接口调试解决方案,proxyTable解决方案
  10. Asp.Net Core 2.0 项目实战(7)MD5加密、AES&DES对称加解密