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这样的外部工具就能完成对接口的测试. 具体来讲,是由 ...
随机推荐
- HttpClient Coder Example
Example 1: HttpClient httpClient = new HttpClient(); httpClient.getHostConfigurati ...
- C# 调用adb command 读取手机型号和IMEI
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- 千万级大数据的Mysql数据库SQL语句优化
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- centos7下安装openvpn,访问内网服务器 (二) windows访问
一.简介 在上一章中已经安装好了openvpn,并且已经启动成功,现在就可以通过openvpn的客户端进行连接访问内网服务器了. 二.安装openvpn客户端 下载地址: https://www.te ...
- McCabe环路复杂度计算方法
环路复杂度用来定量度量程序的逻辑复杂度.以McCabe方法来表示. 在程序控制流程图中,节点是程序中代码的最小单元,边代表节点间的程序流.一个有e条边和n个节点的流程图F,可以用下述3种方法中的任何一 ...
- CCS调试教程
包括CCS3.3和CCS5.5两个版本的调试教程. CCS3.3 3.3教程来自http://zhujlhome.blog.163.com/blog/static/205621092201261032 ...
- 解决带fusionCharts的页面多次点击后不显示的问题
问题: 假设不使用公司封装的fusioncharts.使用自己定义的.建议不要使用例如以下方法 使用$(document).ready( 页面载入完之后再载入,会导致多次点击带有fusionchart ...
- 43. Spring Boot动态数据源(多数据源自动切换)【从零开始学Spring Boot】
[视频&交流平台] àSpringBoot视频 http://study.163.com/course/introduction.htm?courseId=1004329008&utm ...
- spring 第一篇(1-1):让java开发变得更简单(上)
1.释放POJOS能量 传统开发中是如何束缚POJOS呢,如果你开发过java很长时间,那你一定有接触过EJB的开发.那时候开发一个小小的功能都要扩展框架的类或者实现其接口.所以你很容易在早期的Str ...
- Blackey win10 + python3.6 + VSCode + tensorflow-gpu + keras + cuda8 + cuDN6N环境配置(转载)
win10 + python3.6 + VSCode + tensorflow-gpu + keras + cuda8 + cuDN6N环境配置 写在前面的话: 再弄这个之前,我对python也好 ...