spring web中完成单元测试
对于在springweb总完单元测试,之前找过些资料,摸索了很久,记录下最终自己使用的方法
1,创建测试类,创建测试资源文件夹 src/test/resources/WEB_INFO/conf
将工程用的spring配置放在这里,
另外在此目录下创建文件InitJndi.xml,其中放入工程使用的数据源信息,内容入下:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" 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-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<beans:bean id="DB1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<beans:property name="url" value="jdbc:oracle:thin:@1.1.1.1:1521:orcl" />
<beans:property name="username" value="db1" />
<beans:property name="password" value="db1" />
</beans:bean>
<beans:bean id="DB2" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<beans:property name="url" value="jdbc:oracle:thin:@//2.2.2.2:1521/ORCL" />
<beans:property name="username" value="db2" />
<beans:property name="password" value="db2" />
</beans:bean>
</beans:beans>
2,在测试类中增加spring文件的引用
@RunWith(SpringJUnit4ClassRunner.class)
@Configuration
@ImportResource({ "classpath:./web-inf/conf/spring.application-context.xml" })
@ContextConfiguration(classes =ParserServiceImplTest.class)
public class ParserServiceImplTest {
... ...
}
3,增加类的beforeClass方法,并在其中完成数据源的引用,如下:
@BeforeClass
public static void beforeClass() throws Exception {
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext(
"classpath:./web-inf/conf/InitJndi.xml"); // 引用数据源配置文件
DataSource DB1= (DataSource) app.getBean("DB1");
DataSource DB2= (DataSource) app.getBean("DB2");
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
builder.bind("DB1", DB1);
builder.bind("DB2", DB2);
builder.activate();
}
在测试方法中完成对应Service的测试工作
@Autowired
IParserService parserService;
@Test
public void handleMessageTest() {
parserService.handleMessage();
}
spring web中完成单元测试的更多相关文章
- 真正意义上的spring环境中的单元测试方案spring-test与mokito完美结合
真正意义上的spring环境中的单元测试方案spring-test与mokito完美结合 博客分类: java 测试 单元测试SpringCC++C# 一.要解决的问题: spring环境中 ...
- spring web中的filter
昨天看了会spring web中部分代码,主要是各种filter,回顾一下: Spring的web包中中有很多过滤器,这些过滤器位于org.springframework.web.filter并且理所 ...
- Spring5源码,Spring Web中的处理程序执行链
一.什么是Spring中的处理程序执行链? 二.HandlerExecutionChain类 三.自定义处理程序执行链 Spring的DispatcherServlet假如缺少几个关键元素将无法分派请 ...
- 如何在spring环境中做单元测试
在测试类的上方加入以下注解 @RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:spring.xm ...
- Spring Security 中的过滤器
本文基于 spring-security-core-5.1.1 和 tomcat-embed-core-9.0.12. Spring Security 的本质是一个过滤器链(filter chain) ...
- 一文带你掌握Spring Web异常处理方式
一.前言 大家好,我是 去哪里吃鱼 ,也叫小张. 最近从单位离职了,离开了五年多来朝朝夕夕皆灯火辉煌的某网,激情也好悲凉也罢,觥筹场上屡屡物是人非,调转过事业部以为能换种情绪,岂料和下了周五的班的前同 ...
- SpringMVC,MyBatis项目中兼容Oracle和MySql的解决方案及其项目环境搭建配置、web项目中的单元测试写法、HttpClient调用post请求等案例
要搭建的项目的项目结构如下(使用的框架为:Spring.SpingMVC.MyBatis): 2.pom.xml中的配置如下(注意,本工程分为几个小的子工程,另外两个工程最终是jar包): 其中 ...
- 46. Spring Boot中使用AOP统一处理Web请求日志
在之前一系列的文章中都是提供了全部的代码,在之后的文章中就提供核心的代码进行讲解.有什么问题大家可以给我留言或者加我QQ,进行咨询. AOP为Aspect Oriented Programming的缩 ...
- 如何对Spring MVC中的Controller进行单元测试
对Controller进行单元测试是Spring框架原生就支持的能力,它可以模拟HTTP客户端发起对服务地址的请求,可以不用借助于诸如Postman这样的外部工具就能完成对接口的测试. 具体来讲,是由 ...
随机推荐
- 豆瓣电台笔记3:cell添加从中间向两侧放大的动画
步骤: 1.设置动画属性的初始值 cell.layer.transform = CATransform3DMakeScale(0.1,0.1,1) 2.在指定时间内执行动画 UIView.animat ...
- Android创建JSON格式数据
Android创建JSON格式数据 作为上一篇博客的补充,简单那解说了一下Android创建JSON格式数据的小Demo. 1. 创建JSON格式数据 对于Android创建JSON格式数据.因为An ...
- hibernate(jpa)中注解配置字段为主键
http://www.blogjava.net/ITdavid/archive/2009/02/25/256605.html 注解方式的主键配置 非自增字段为主键,注解annotation表示 ...
- Failed to read auto-increment value from storage engine
今天在使用php artisan db:seed进行填充1000条数据时,出现如下错误Failed to read auto-increment value from storage engine 原 ...
- iOS定位服务CoreLocation
欢迎訪问我的新博客: 开发人员说 基于LBS的应用开发是当今移动开发中的一大热门, 当中主要涉及到地图和定位两个方面. iOS开发中, 定位服务依赖于CoreLocation框架, CLLocatio ...
- nginx 404重定向到自定义页面
在访问时遇到上面这样的404错误页面,我想99%(未经调查,估计数据)的用户会把页面关掉,用户就这样悄悄的流失了.如果此时能有一个漂亮的页面能够引导用户去他想去的地方必然可以留住用户.因此,每一个网站 ...
- 【应用篇】Activiti外置表单实例demo(四)
在这里我想说的外置表单.是说我们将我们自己的jsp(.form,.html)等页面上传到工作流的数据库中,当任务运行到当前结点时.给我们像前台发送绑定好的表单. 此处是给表单绑定表单的过程 water ...
- pthread_cleanup_push和pthread_cleanup_pop清除函数是否执行的说明
示例1: #include <stdio.h> #include <pthread.h> void* clean(void* arg) { printf("clean ...
- 让你的程序通过XP防火墙
procedure TForm1.Button1Click(Sender: TObject); var FwMgr,Profile,FwApp: variant; begin FwMgr := Cre ...
- UI-1-UI入门
课程要点: 创建一个iOS工程 AppDelegate类 UIKit框架以及UIWindow 在window上添加第一个试图UIView NSTimer(定时器) 创建一个iOS工程 PS:接下来简单 ...