将基于Spring的UnitTest集成到Maven中
在进行persistence layer的开发时,我们总是要写写UnitTest来验证我们提供给上一层的API是经过验证了的。通常我们需要单独的为UnitTest编写一份独立的Spring ApplicationContext配置文件,基于Maven约定大于配置的实践,我们将Spring ApplicationContext配置文件放在项目的src/test/resources下,Test Source Code放在sr/test/java下。
Spring为我们提供一个非常方便的Annotation类@ContextConfiguration,可以使用该Annotation告诉Spring将在何处载入ApplicationContext配置文件以便运行测试代码。将此Annotation配置在UnitTest类声明上,则UnitTest类就可方便地利用Spring来编写测试用例了。
下面为一UnitTest实例:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:/forum-repository-test.xml" })
public class JpaUserRepositoryTest {
@Autowired
private UserRepository userRepository;
@Test
public void testGetUserByName() {
String name = "Wang lu";
User user = userRepository.getUserByName(name);
Assert.assertEquals(name, user.getUserName());
}
}
如果在运行Unitest中需要loadtimeweaver功能(比如你的ORM框架使用的是eclipselink),则在Maven的pom中需要添加如下plugin并指定argLine,将javaagent指向spring-agent.jar。
<build>
…
<plugins>
…
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<configuration>
<forkMode>once</forkMode>
<argLine>
-javaagent:{spring-agent.jar path}
</argLine>
<useSystemClassloader>true</useSystemClassloader>
</configuration>
</plugin>
</plugins>
</build>
以下为Spring ApplicationContext测试配置文件:
<?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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
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-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="com.lulusoft.forum.infrastructure.repositories"></context:component-scan>
<bean id="frmDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="oracle.jdbc.driver.OracleDriver"
p:url="jdbc:oracle:thin:@//xiechangming:1521/xiech2" p:username="htl_owner"
p:password="htlowner" />
<bean id="forumEntityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="forumPU" />
<property name="dataSource" ref="frmDataSource" />
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
<property name="generateDdl" value="false" />
<property name="database" value="ORACLE" />
<property name="databasePlatform"
value="org.eclipse.persistence.platform.database.oracle.Oracle11Platform" />
</bean>
</property>
</bean>
</beans>
将基于Spring的UnitTest集成到Maven中相关推荐
- 基于spring mvc+bootstrap 集成的返利平台 新增内容-自动对接京东数据
本系统基于最新版的spring mvc+adminlte构成,使用ant来做打包工具.在系统中,大量使用了开源的代码,再次特别感谢这些开源工作者. 后端使用了 spring mvc mybatis,目 ...
- 基于Spring Boot的教学论坛系统中回复功能的实现
基本简介: 对一个论坛网站来说,回复功能是一个很重要的功能,它可以实现用户与用户之间关于所发帖子内容的沟通与交流. 首先,我们先看一下回复功能的实现页面: 主要步骤: (一)搭建Comment数据库 ...
- SpringBoot2.x系列教程(六十六)Spring Boot快速集成RocketMQ实战教程
前言 RocketMQ是目前主流的消息中间件之一,并且自身就支持分布式功能.最初由阿里巴巴团队开发,并且经历过双十一等海量消息场景的考验,后捐赠给Apache开源基金会,这也是为什么我们经常听说Roc ...
- 基于spring的系统设计与开发ADD ME
什么是基于spring框架的设计,基于spring框架的设计究竟有什么用呢?我们可以用基于spring框架的设计来做些什么呢?这是我今天要说的内容. 当然,百度基于spring框架的设计会出现一大堆基 ...
- maven多模块项目部署到服务器,GitHub - baxias/foweb: 一个基于 Spring+SpringMVC+Mybatis 的Maven多模块项目。(实现前后端分离的服务器端)...
Foweb Framework A multi-modules maven project base on Spring+SpringMVC+Mybatis. 一个基于 Spring+SpringMV ...
- springboot毕设项目基于Spring Boot的智慧天气管理系统84z99(java+VUE+Mybatis+Maven+Mysql)
springboot毕设项目基于Spring Boot的智慧天气管理系统84z99(java+VUE+Mybatis+Maven+Mysql) 项目运行 环境配置: Jdk1.8 + Tomcat8. ...
- 基于Spring Boot+Cloud构建微云架构
链接:my.oschina.net/u/3636867/blog/1802517 前言 首先,最想说的是,当你要学习一套最新的技术时,官网的英文文档是学习的最佳渠道.因为网上流传的多数资料是官网翻译而 ...
- 基于Spring Boot和Spring Cloud实现微服务架构学习--转
原文地址:http://blog.csdn.net/enweitech/article/details/52582918 看了几周spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习 ...
- 基于 Spring Boot 和 Spring Cloud 实现微服务架构
前言 首先,最想说的是,当你要学习一套最新的技术时,官网的英文文档是学习的最佳渠道.因为网上流传的多数资料是官网翻译而来,很多描述的重点也都偏向于作者自身碰到的问题,这样就很容易让你理解和操作出现偏差 ...
最新文章
- 深入学习Redis高可用架构:哨兵原理及实践
- 软件体系结构的风格(转载)
- 利用二级指针删除单向链表
- 深度学习简介(一)——卷积神经网络
- Mysql字符串截取 mysql将字符串字段转为数字排序或比大小
- oracle中时间加减一年的写法
- PCR之父凯利·穆利斯:有才,真的可以为所欲为
- Java基础:常用的API
- 【UE4】 Pak解密、挂载、加载
- JDK源码——JDK8源码编译全流程(linux)
- editplus mysql模板_editplus下载
- 面试中如何回答“你的缺点是什么?”
- 2022年,在NLP中还有没有比较新的研究方向?
- win10计算机丢失msvcr,win10系统运行程序提示计算机中丢失msvcr110.dll的教程
- 基于java(ssm)留学生交流互动论坛系统源码(java毕业设计)
- Oracle 20c 不再支持特性:传统审计不支持 统一审计(Unified Auditing)成主流
- 上海海洋大学计算机考研资料汇总
- C语言中fclose函数
- 原生js实现字幕滚动
- switch模式的切换
热门文章
- android 日倒计时计算器,分秒倒数日app下载-分秒倒数日(时间计算器)安卓版下载 v2.0.0安卓版_5577安卓网...
- 亲述真实经历--“人工智能培训机构”的“坑”,希望更多像我这样的人不要吃了第一次亏才后知后觉
- 人工智能相关复试内容
- [坑]删除我的电脑中的各种云盘图标(WPS云、微盘云...)
- 鸿蒙系统图片大全,江南百景图鸿蒙版
- 为什么不能实现精确扫频?
- SourceInsight4 破解版安装
- mysql goldengate_Goldengate完成Mysql到Mysql的数据同步
- 精密加工与超精密加工的一些问题整理
- eclipse php设置xdebug,eclipse 中配置php的 XDebug调试