1.AbstractTransactionalJUnit4SpringContextTests 和AbstractJUnit4SpringContextTests。我们在测试用例类要继承两种中的一个。

AbstractTransactionalJUnit4SpringContextTests提供了数据库自动回滚,也就是说测试前和测试后数据库是一样的

AbstractJUnit4SpringContextTests不提供数据库自动回滚,测试会破坏数据库。

查看源码发现,AbstractTransactionalJUnit4SpringContextTests就是继承自AbstractJUnit4SpringContextTests,同时类上面多了两个Annotation:@TestExecutionListeners(TransactionalTestExecutionListener.class) 和@Transactional。所以实现数据库回滚有两种方法,如下:

public class BasicTest extends AbstractTransactionalJUnit4SpringContextTests {}

和

@TestExecutionListeners(TransactionalTestExecutionListener.class)
@Transactional
public class BasicTest extends AbstractJUnit4SpringContextTests {
}

好吧,说了这么多没用的还不如直接上代码

为了避免每个测试用例都要配置spring环境,先建立一个父类:BasicTest,内容很简单,真的很简单:

@ContextConfiguration(locations= {"classpath:spring/applicationContext.xml","classpath:spring/spring-servlet.xml"})
public class BasicTest extends AbstractTransactionalJUnit4SpringContextTests { }
@ContextConfiguration:顾名思义就是配置context配置文件的地址,我这里是Web程序,所以还要spring-servlet.xml。
网上的例子都有@RunWith注解,但是我发现 AbstractJUnit4SpringContextTests 类上已经有了@RunWith,那么这里就不需要了。只要配置@ContextConfiguration就可以了 然后测试用例继承BasicTest如下: 具体的测试方法怎么写就不用说了。
public class PeopleServiceTest extends BasicTest{

    @Resource(name="PeopleService")
private PeopleService ps; @Test
public void queryAll() {
List<People> list=ps.queryAll(null);
System.out.println(list);
Assert.assertEquals("list没有10个", , list.size());
} @Test
public void update() {
People p=new People().set("id", ).set("name", "tom").set("address", "").set("age", );
Assert.assertEquals("没有修改", , this.ps.update(p));
}
@Test
public void insert() {
People p=new People().set("name", "Mike").set("address", "america").set("age", );
Assert.assertEquals("没有插入", , this.ps.add(p));
}
}

 

spring 4.0 JUnit简单的Dao,Service测试的更多相关文章

  1. spring 4.0 JUnit简单的Controller测试

    比Dao和Service的测试稍微复杂一点.还是先写一个BasicWebTest用来总体配置: @WebAppConfiguration @ContextConfiguration(locations ...

  2. ssh框架整合---- spring 4.0 + struts 2.3.16 + maven ss整合超简单实例

    一 . 需求 学了这么久的ssh,一直都是别人整合好的框架去写代码,自己实际动手时才发现框架配置真是很坑爹,一不小心就踏错,真是纸上得来终觉浅! 本文将记录整合struts + spring的过程 , ...

  3. 使用spring 4.0 + maven 构建超简单的web项目

    一.需求 使用spring去管理web项目,是目前非常流行的一种思路,本文将介绍使用maven+spring 4.0.2 来构建一个简单的web项目. 二.实现 1.新建一个maven项目,如下图所示 ...

  4. spring boot web开发 简单的增删改查和spring boot 自带的Junit测试 案例

    创建 web项目 配置pom.xml文件   ------相当于jar包 配置application.yml -----配置文件(spring数据库连接.server服务.logging日志等) 创建 ...

  5. Spring的事务到底该给Dao配置还是给Service配置

    Spring的事务到底该给Dao配置还是给Service配置 Spring事务为业务逻辑进行事务管理,保证业务逻辑上数据的原子性. 事务得根据项目性质来细分:事务可以设置到三个层面(dao层.serv ...

  6. SSM框架整合(Spring+SrpingMVC+Mybatis) 简单案例

    简介: SSM框架是Spring,SpringMVC 和Mybatis框架的整合,是标准的MVC模式,将整个系统划分为表现层,controller层,service层,dao层四层. Spring实现 ...

  7. 详解 Spring 3.0 基于 Annotation 的依赖注入实现(转)

    使用 @Repository.@Service.@Controller 和 @Component 将类标识为 Bean Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的 ...

  8. Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(三)

    前两章我为大家详细介绍了如何搭建Maven环境.Spring MVC的流程结构.Spring MVC与Struts2的区别以及示例中的一些配置文件的分析.在这一章,我就对示例的层次结构进行说明,以及M ...

  9. 详解 Spring 3.0 基于 Annotation 的依赖注入实现--转载

    使用 @Repository.@Service.@Controller 和 @Component 将类标识为 Bean Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的 ...

随机推荐

  1. hadoop 根据secondary namenode恢复namenode

    refer to http://www.cnblogs.com/Richardzhu/p/3435989.html http://blog.csdn.net/wuzhilon88/article/de ...

  2. Data Structure Linked List: Reverse a Linked List in groups of given size

    http://www.geeksforgeeks.org/reverse-a-list-in-groups-of-given-size/ #include <iostream> #incl ...

  3. Centos7 配置yum源 安装epel

    一.什么是epel如果既想获得 RHEL 的高质量.高性能.高可靠性,又需要方便易用(关键是免费)的软件包更新功能,那么 Fedora Project 推出的 EPEL(Extra Packages ...

  4. <Perl算法小菜>排序加速--Schwatzian变换及Guttman-Rosler变换

    原创博客,转载请联系博主! perl里的数据都是以双精度为单元存储的,也就是相当于C/Cpp中的double型,而正则的解析是由perl内置的正则引擎完成的,那么除了重写一个属于自己的排序方法之外,我 ...

  5. Sqoop- sqoop将mysql数据表导入到hive报错

    sqoop将mysql数据表导入到hive报错 [root@ip---- lib]# sqoop import --connect jdbc:mysql://54.223.175.12:3308/gx ...

  6. 纯CSS3跳动焦点广告轮播特效

    1. [代码] 纯CSS3跳动焦点广告轮播特效 <!--  Author: Developed by Caleb Jacob Author Website: http://iamceege.co ...

  7. 异步刷新页面的前进与后退的实现--pushState replaceState

    实现目标 页面的跳转(前进后退,点击等)不重新请求页面 页面URL与页面展现内容一致(符合人们对传统网页的认识) 在不支持的浏览器下降级成传统网页的方式 使用到的API history.state 当 ...

  8. java: jsp:param中文乱码

    java: jsp:param中文乱码 假如a.jsp/b.jsp文件中 a.jsp代码: 需要加入:request.setCharacterEncoding("UTF-8")  ...

  9. Delphi 实现检测线程类TThread是否结束

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  10. Delphi中TList类应用

    在DELPHI中指针最常见的就是和类TLIST结合起来使用.下面是一个很简单的例子,希望对这个例子的分析能让大家对使用TLIST类有一个简单的认识. 代码的功能是使用指针和Tlist来生成一个牌串,并 ...