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. }

使用Spring+Junit4.4进行测试的更多相关文章

  1. 使用Spring+Junit4.4进行测试(使用注解)

    http://nottiansyf.iteye.com/blog/345819 使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunn ...

  2. 用Spring+Junit4.4进行测试(使用注解)

    http://nottiansyf.iteye.com/blog/345819 使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunn ...

  3. 使用 Spring 2.5 TestContext 测试框架

    Spring 2.5 TestContext 测试框架用于测试基于 Spring 的程序,TestContext 测试框架和低版本 Spring 测试框架没有任何关系,是一个全新的基于注解的测试框架, ...

  4. Spring框架下Junit测试

    Spring框架下Junit测试 一.设置 1.1 目录 设置源码目录和测试目录,这样在设置产生测试方法时,会统一放到一个目录,如果没有设置测试目录,则不会产生测试代码. 1.2 增加配置文件 Res ...

  5. Spring Boot应用的测试——Mockito

    Spring Boot应用的测试——Mockito Spring Boot可以和大部分流行的测试框架协同工作:通过Spring JUnit创建单元测试:生成测试数据初始化数据库用于测试:Spring ...

  6. Spring Junit4 接口测试

    Junit实现接口类测试 - dfine.sqa - 博客园http://www.cnblogs.com/Automation_software/archive/2011/01/24/1943054. ...

  7. spring boot项目如何测试,如何部署

    有很多网友会时不时的问我,spring boot项目如何测试,如何部署,在生产中有什么好的部署方案吗?这篇文章就来介绍一下spring boot 如何开发.调试.打包到最后的投产上线. 开发阶段 单元 ...

  8. Spring、Spring Boot和TestNG测试指南 - 使用Spring Boot Testing工具

    Github地址 前面一个部分讲解了如何使用Spring Testing工具来测试Spring项目,现在我们讲解如何使用Spring Boot Testing工具来测试Spring Boot项目. 在 ...

  9. Spring Boot中的测试

    文章目录 简介 添加maven依赖 Repository测试 Service测试 测试Controller @SpringBootTest的集成测试 Spring Boot中的测试 简介 本篇文章我们 ...

随机推荐

  1. SpringBoot学习之集成dubbo

    一.摘自官网的一段描述 1.背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 单一应用架构 ...

  2. Requests库介绍

    Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTT ...

  3. 记一次java heap space的解决办法

    问题缘由:后台上传excel导入到数据库,数据量太大,导致报错. 解决方案: 用jdk自带的性能分析器(jconsole)查看了一下,当excel开始导入的时候,发现堆空间直接爆掉. 增加堆空间,在c ...

  4. prop与attr的区别

    与prop一样attr也可以用来获取与设置元素的属性. 区别在于,对于自定义属性和选中属性的处理. 选中属性指的是 checked,selected 这2种属性 1. 对于自定义属性 attr能够获取 ...

  5. 了解ASCII、gb系列、Unicode、UTF-8的区别

    转自:http://www.douban.com/note/334994123/?type=rec ● 为什么有这么多编码? ● UTF-8和GB2312有什么区别? ● 我们在国内做网站是用UTF- ...

  6. 关于一些基础的Java问题的解答(七)

    31. 反射的作用与原理 简单的来说,反射机制其实就是指程序在运行的时候能够获取自身的信息.如果知道一个类的名称或者它的一个实例对象, 就能把这个类的所有方法和变量的信息(方法名,变量名,方法,修饰符 ...

  7. [ SSH框架 ] Hibernate框架学习之二

    一.Hibernate持久化类的编写规范 1.什么是持久化类 Hibernate是持久层的ORM影射框架,专注于数据的持久化工作.所谓持久化,就是将内存中的数据永久存储到关系型数据库中.那么知道了什么 ...

  8. 70. Climbing Stairs(easy, 号称 Dynamic Programming 天下第一题)

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  9. centos 挂载ntfs格式的移动硬盘

    经查找资料发现,linux也是可以支持ntfs格式分区的,只是需要安装ntfs-3g插件. CentOS挂载ntfs移动硬盘的具体步骤: 1 安装fuse. 下载fuse-2.9.3.tar.gz   ...

  10. 微信小程序 --- 无法跳转到tab页面问题

    首先检查你的跳转方法,如果是wx.navigateTo(OBJECT)或者是wx.redirectTo(OBJECT)都是无法跳转的,在微信小程序中如果需要跳转到具有tab的页面必须使用wx.swit ...