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这样的外部工具就能完成对接口的测试. 具体来讲,是由 ...
随机推荐
- JavaScript | 基础表单验证(纯Js)
———————————————————————————————————————————— 基础表单验证(纯js) - - - - - - - - - - - - - - - - - - - - - - ...
- struts上传文件 血案
记录一个图片上传之后没有后缀 拓展名问题 平常我们查询数据都是 fileImage=fileImageService.getQuery(); 让entity等于它 那么fileImage.getF ...
- OUTLOOK之不能直接发送邮件的Mapi错误解决方法
近期在从office2010直接升级到2013,发现右键文件不能发送到邮件了,同一时候在word文档里.也不能作为附件发送到邮件了. 提示错误是: Mapi Failure - "Unspe ...
- Java 8 Lambda表达式介绍
Lambda是什么? Lambda是一个匿名函数,我们可以把Lambda理解为是一段可以传递的代码.可以写出简洁.灵活的代码.作为一种更紧凑的代码风格,使java的语言表达能力得到提升. 可以这么说l ...
- GoWeb编程之HelloWorld
使用Go实现我们的第一个Web服务器程序 package main import "net/http" import "fmt" //打印HelloWorld ...
- 八款开源 Android 游戏引擎[转]
记录一下,以备不时之需~~~~~ 虽然android学了点点,然后现在又没学了(我为啥这么没有恒心呢大哭).以后有时间还是要继续学android的,一定要啊!虽然现在没学android游戏编程,不过还 ...
- 关于搭建HTTPS服务器服务
关于 HTTPS 的基本原理大家都已经不再陌生,今天和大家说说如何搭建一个支持 HTTPS 的服务端. 服务端的 HTTPS HTTPS 已经几乎成为了当前互联网推荐的通信方式,它能最大化保证信息传输 ...
- apple touch icon 大小总结
<!-- For Chrome for Android: --> <link rel="icon" sizes="192x192" href= ...
- Beamer加中文
\documentclass{beamer} \mode<presentation> { \usetheme{CambridgeUS} % or try Darmstadt, Madrid ...
- merge实现拉链表
建表如下( 历史拉链表): 新表(每日更新的): 实现语句: MERGE INTO test_target t1 USING ( SELECT nvl(c.id, b.id) AS id ,CASE ...