Spring项目使用Junit4测试配置
我们经常要写junit测试,在不启动整个web项目的情况下,测试自己的service实现或者是dao实现,我们来充分利用下junit4的强大功能。
1.junit4的测试类
import java.util.ArrayList; import java.util.List;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.annotation.Rollback; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.founder.mrp.domain.webservice.ErpProductXml; import com.founder.mrp.domain.webservice.ErpTypeSetInfoXml; import com.founder.mrp.service.webservice.ErpTypeSetProductService;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:spring-*.xml"}) public class JunitWebservice {@AutowiredErpTypeSetProductService erpTypeSetProductService;@Test@Rollback(false)public void TestErpTypeSetInfoXml(){List<ErpTypeSetInfoXml> erpTypeSetInfoXmlList = new ArrayList<ErpTypeSetInfoXml>();ErpTypeSetInfoXml erpTypeSetInfoXml = new ErpTypeSetInfoXml(); erpTypeSetInfoXml.setAUFNR("1000021571");erpTypeSetInfoXml.setZ_EDT_2000("ty00001");erpTypeSetInfoXml.setZ_EDT_2002("试试书");erpTypeSetInfoXml.setZ_EDT_2003("t00001");erpTypeSetInfoXml.setZ_EDT_2008("1");ErpTypeSetInfoXml erpTypeSetInfoXml2 = new ErpTypeSetInfoXml();erpTypeSetInfoXml2.setAUFNR("1000021582");erpTypeSetInfoXml2.setZ_EDT_2000("ty00002");erpTypeSetInfoXml2.setZ_EDT_2002("测试计划");erpTypeSetInfoXml2.setZ_EDT_2003("t00002");erpTypeSetInfoXml2.setZ_EDT_2008("1");erpTypeSetInfoXmlList.add(erpTypeSetInfoXml);erpTypeSetInfoXmlList.add(erpTypeSetInfoXml2);erpTypeSetProductService.saveTypeSetInfo(erpTypeSetInfoXmlList);}@Test@Rollback(false)public void TestErpProductXml(){List<ErpProductXml> erpProductXmlList = new ArrayList<ErpProductXml>();ErpProductXml erpProductXml = new ErpProductXml();erpProductXml.setAUFNR("5000022550");erpProductXml.setZ_EDT_2000("pr00001");erpProductXml.setZ_EDT_2001("神射手");erpProductXml.setZ_PUB_5928("测试印刷厂");erpProductXml.setZ_EDT_2003("p00001");erpProductXml.setZ_PUB_5008("100");ErpProductXml erpProductXml2 = new ErpProductXml();erpProductXml2.setAUFNR("5000022551");erpProductXml2.setZ_EDT_2000("pr00002");erpProductXml2.setZ_EDT_2001("水电费科技");erpProductXml2.setZ_PUB_5928("测试印刷厂");erpProductXml2.setZ_EDT_2003("p00002");erpProductXml2.setZ_PUB_5008("100");erpProductXmlList.add(erpProductXml);erpProductXmlList.add(erpProductXml2);erpTypeSetProductService.saveErpProduct(erpProductXmlList);}}
2.解释常用到的注解
@RunWith(SpringJUnit4ClassRunner.class)SpringJUnit支持,由此引入Spring-Test框架支持! @ContextConfiguration(locations = "classpath:applicationContext.xml") 多个配置文件的话可以用数组表示{“applicationContext.xml”,“applicationContext1.xml”}; @ContextConfiguration("/spring-context.xml")放在根路径下(即类路径下),然后<import resource="spring-dao.xml" />所有的配置文件和资源文件 @Transactional这个非常关键,如果不加入这个注解配置,事务控制就会完全失效! @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)这里的事务关联到配置文件中的事务控制器(transactionManager = "transactionManager"), 同时指定自动回滚(defaultRollback = true)。这样做操作的数据才不会污染数据库! AbstractTransactionalDataSourceSpringContextTests要想构建这一系列的无污染纯绿色事务测试框架就必须找到这个基类!(即所有事务均不生效) @ActiveProfiles(value="dev")配置环境选择然后在方法上使用@Test,@RollBack,@Transaction等注解单独修饰
转载于:https://www.cnblogs.com/loong-hon/p/10334063.html
Spring项目使用Junit4测试配置相关推荐
- vue H5移动端项目 真机测试配置
vue cli2 1.配置网段:(手机和电脑在同一局域网内) 2.在项目中重启命令行(或者cmd)运行 ipconfig 获取到项目在本机运行的ip地址 3.关闭防火墙 4.前端:config文件夹下 ...
- 如何在Spring和Spring MVC项目中进行测试
Spring框架概述 Spring大约包含了20个模块,这些模块组成了核心容器(Core Container).数据访问/集成(Data Access/Integration).Web.AOP(面向切 ...
- spring 项目集成配置_Spring重试–与项目集成的方式
spring 项目集成配置 如果您需要在代码中实现健壮的重试逻辑,一种行之有效的方法是使用spring重试库. 我的目的不是要展示如何使用spring retry项目本身,而是要演示将其集成到代码库中 ...
- JAVAWEB开发之Spring详解之——Spring的入门以及IOC容器装配Bean(xml和注解的方式)、Spring整合web开发、整合Junit4测试
Spring框架学习路线 Spring的IOC Spring的AOP,AspectJ Spring的事务管理,三大框架的整合 Spring框架概述 什么是Spring? Spring是分层的Java ...
- Spring入门详解(一)如何配置一个简单的spring项目
关于spring的一些概念,网上已经有很多的说明,本系列不会做太多的描述,重心在如何配置上. 一.准备工作 1.安装JDK,配置环境.(本来不想写这条的-想了想,还是写上) 2.下载开发包 Sprin ...
- eclipse创建springboot项目_创建SpringBoot自动配置项目:Starter测试使用
Starter 测试使用 完成了 starter 项目的创建.发布之后,在 Spring Boot 项目中便可以直接使用了,下面简单介绍一-下 Starter 测试使用步骤,其中省略掉了 Spring ...
- SSM4==通过XML配置spring 事务,通过纯注解配置spring 事务
mysql的事务管理,是指在有关系的表(比如有外键)之间实现ACID,比如表A的金额列的外键是表B的金额列,那么如果A的金额减了300,B的金额也要减少300.如果A减300成功了,B失败了,mysq ...
- 使用 Spring 2.5 TestContext 测试框架
Spring 2.5 TestContext 测试框架用于测试基于 Spring 的程序,TestContext 测试框架和低版本 Spring 测试框架没有任何关系,是一个全新的基于注解的测试框架, ...
- Spring基于注解TestContext 测试框架使用详解
原创整理不易,转载请注明出处:Spring基于注解TestContext 测试框架使用详解 代码下载地址:http://www.zuidaima.com/share/1775574182939648. ...
最新文章
- ffmpeg architecture(中)
- Unity 中的协同程序
- SQLite关于时间段查询的sql
- 框架--NoHttp和OkHttp哪个好用,Volley和NoHttp哪个好用?
- Android中View绘制流程
- 实操案例:字符串哈希表操作
- 万事无忧之看看网站的PR值
- 转载:cisco voip转码设置
- 【转】Roberts 算子
- 阶段3 1.Mybatis_11.Mybatis的缓存_2 延迟加载和立即加载的概念
- 简述html文档的基本,网页制作简答题答案
- 2021年初行政区划数据
- 经典C语言编程100例——题目+答案代码(1-10)
- 【苹果相册】苹果推信群发准入ProvisioningProfile还分为开发和分发
- 酒吧空间怎样量身打造
- 笔记本电脑没声音的解决方法教程
- TikZ绘制多样的花括号
- 教程 | 阿克曼结构移动机器人的gazebo仿真(二)
- Eclipse日志文件存放在何处
- 学习 VS2010编程经典示例
热门文章
- ACM 整数划分(四)
- struts1.3.8与hibernate3.2.5整合所遇到的问题
- Django,js,html数据传输
- OpenCV-python学习笔记(五)——shreshoding阈值化
- 分析频域滤波和空域滤波的各自特点_IIR数字滤波器设计(数字信号处理)
- The Generalist和Visualize Value在去中心化内容发布平台Mirror上发起众筹
- 加密托管机构Komainu与英国当局合作存储没收的加密货币
- SAP License:SAP工业4.0 创新智能物流解决方案
- 二代征信在小额线上贷款风控领域应用探索
- 智慧、智能工业园区管理平台+登记管理+人行闸管理+车辆闸管理+统计分析+系统管理+地理信息+Axure高保真web端后台管理系统