Spring 使用介绍(十)—— 单元测试
一、概述
Spring测试框架提供了对单元测试的支持,以便使用spring的依赖注入和事务管理功能
maven依赖:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
二、简单示例
业务接口及实现类
public interface UserService {
void addUser(String name, int age);
void updateUserName(String name);
}
@Service
public class UserServiceImpl implements UserService { @Autowired
private DataSource dataSource; public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
} @Transactional(propagation = Propagation.REQUIRED)
@Override
public void addUser(String name, int age) {
String sql = String.format("INSERT INTO `user`(user_name, age) VALUES('%s', %d)", name, age);
new JdbcTemplate(dataSource).update(sql); this.updateUserName(name + "_" + name);
} @Transactional(propagation = Propagation.REQUIRED)
@Override
public void updateUserName(String name) {
String sql = String.format("UPDATE `user` SET user_name = '%s'", name);
new JdbcTemplate(dataSource).update(sql); // throw new RuntimeException("9965");
}
}
XML配置
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- 数据源 -->
<bean id="mysql" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
...
</bean> <!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="mysql"/>
</bean> <!-- 开启事务注解支持 -->
<tx:annotation-driven transaction-manager="transactionManager"/> <context:component-scan base-package="cn.matt.transaction"/>
</beans>
测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-context.xml")
// @TransactionConfiguration(transactionManager = "txManager", defaultRollback=true)
public class BaseSpringTest { }
public class TransactionConfigTest extends BaseSpringTest { @Autowired
UserService userService; @Test
@Transactional
public void testTransactionConfig() {
userService.addUser("jerry", 30);
}
}
三、配置说明:
- @RunWith:用于指定junit运行环境,spring提供SpringJUnit4ClassRunner作为Junit测试环境,方便使用spring的依赖注入
- @ContextConfiguration:导入Spring配置文件
- @TransactionConfiguration:开启测试类的事务管理支持配置,并指定事务管理器和默认回滚行为,一般无须配置
- @Transactional:表示事务支持,指定方法执行完后自动回滚,可使用在类和方法上
- 使用在方法上,表示该方法获得事务支持
- 使用在类上,表示测试类的所有方法默认获得事务支持
- @Rollback:事务回滚注解,默认为true,可省略,若需要提交事务,须设为false,如下:
public class TransactionConfigTest extends BaseSpringTest {
@Autowired
UserService userService; @Test
@Transactional
@Rollback(false)
public void testTransactionConfig() {
userService.addUser("jerry", 30);
}
}
参考:
第十三章 测试 之 13.1 概述 13.2 单元测试 ——跟我学spring3
第十三章 测试 之 13.3 集成测试 ——跟我学spring3
Spring 使用介绍(十)—— 单元测试的更多相关文章
- [翻译]Spring框架参考文档(V4.3.3)-第二章Spring框架介绍 2.1 2.2 翻译--2.3待继续
英文链接:http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/overview.ht ...
- spring boot / cloud (十四) 微服务间远程服务调用的认证和鉴权的思考和设计,以及restFul风格的url匹配拦截方法
spring boot / cloud (十四) 微服务间远程服务调用的认证和鉴权的思考和设计,以及restFul风格的url匹配拦截方法 前言 本篇接着<spring boot / cloud ...
- spring boot / cloud (十五) 分布式调度中心进阶
spring boot / cloud (十五) 分布式调度中心进阶 在<spring boot / cloud (十) 使用quartz搭建调度中心>这篇文章中介绍了如何在spring ...
- spring boot / cloud (十二) 异常统一处理进阶
spring boot / cloud (十二) 异常统一处理进阶 前言 在spring boot / cloud (二) 规范响应格式以及统一异常处理这篇博客中已经提到了使用@ExceptionHa ...
- Spring Cloud(十二):分布式链路跟踪 Sleuth 与 Zipkin【Finchley 版】
Spring Cloud(十二):分布式链路跟踪 Sleuth 与 Zipkin[Finchley 版] 发表于 2018-04-24 | 随着业务发展,系统拆分导致系统调用链路愈发复杂一个前端请 ...
- Spring 的介绍和目标
1. Spring介绍 打开Spring 官网查看对 Spring 的介绍和目标 http://www.springsource.org/about We believe that: · J2EE s ...
- Spring Boot(二十):使用spring-boot-admin对spring-boot服务进行监控
Spring Boot(二十):使用spring-boot-admin对spring-boot服务进行监控 Spring Boot Actuator提供了对单个Spring Boot的监控,信息包含: ...
- Spring Cloud(十):服务网关 Zuul(路由)【Finchley 版】
Spring Cloud(十):服务网关 Zuul(路由)[Finchley 版] 发表于 2018-04-23 | 更新于 2018-05-09 | 通过之前几篇 Spring Cloud 中 ...
- 【spring boot】10.spring boot下的单元测试
spring boot下的单元测试,思前想后还是需要单独用一章篇幅来看看. 然后在看了介绍和使用时候,我感觉并不想多去看了. 但是还是给后来人留下参考的路径: 官网说明:https://spring. ...
随机推荐
- 微信小程序项目实战 - 菜谱大全
1. 项目简介 最近研究小程序云开发,上线了一个有关菜品查询的小程序.包括搜索.分享转发.收藏.查看历史记录等功能.菜谱 API 来自聚合数据.云开发为开发者提供完整的云端支持,弱化后端和运维概念,无 ...
- 《React Native 精解与实战》书籍连载「Node.js 简介与 React Native 开发环境配置」
此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...
- 阿里巴巴2017实习生招聘模拟题(部分)---C++后台开发方向
1.一个机器人玩抛硬币的游戏,一直不停的抛一枚不均匀的硬币,硬币有A,B两面,A面的概率为3/4,B面的概率为1/4.问第一次出现连续的两个A年的时候,机器人抛硬币的次数的期望是多少? 9/4 11/ ...
- Python_每日习题-0008-九九乘法表
题目: 输出9*9乘法口诀表. 程序分析:分行与分列的考虑,共9行9列,i控制行,j控制列. for i in range(1, 10): for j in range(1, i+1): print( ...
- java的instanceof关键字
java 中的instanceof 运算符是用来判断对象是否是 特定类或这个特定类的子类 的一个实例. 用法: result = object instanceof class 参数: Result: ...
- 福州大学软件工程1816 | W班 第10次作业[软件工程实践总结]
作业链接 个人作业--软件工程实践总结 评分细则 本次由五个问题(每个十分)+创意照片(五分)+附加题(十分)组成 评分统计图 千帆竞发图 汇总成绩排名链接 汇总链接
- shell脚本--操作MySQL数据库
其实就是一个很简单的套路,和其他语言差不多,首先连接数据库,然后在进行其他操作. 套路如下: #!/bin/bash mysql="mysql -uroot -proot" #连接 ...
- react中如何使用动画效果
在react中想要加入动画效果 需要引入 import {CSSTransitionGroup} from 'react-transition-group' //加入react 动画包 import ...
- vue处理异步数据踩过的坑
在开发时,由于数据是异步的导致页面在render 时data是空值 出现报错和警告. 我是这么处理的 把data先写出一个空的完整结构.暂时是这么处理 或者用三元表达式进行赋值监听.data ?myd ...
- tomcat server.xml各个端口的作用
<Server port="8005" shutdown="SHUTDOWN"> <!-- port:指定一个端口,这个端口负责监听关闭Tom ...