Spring Test+JUnit4整合使用测试ZZJ_淘淘商城项目:day01(RESTful Web Service)
针对整合的Dao层与Service层,在做spring与通用Mapper和分页插件相关测试时比较麻烦。如果只用JUnit测试,需要每次Test方法里初始化一下applicationContext,效率比较低下。并且还要进行强制类型转换的造型操作。使用Spring测试套件,测试用例类中的属性会被自动填充Spring容器的对应Bean,直接使用@Autowired注入使用即可!
本次测试目的是看看通用Mapper插件具体如何整合传统SSM项目。
在maven的pom.xml中加入以下配置,其他spring相关配置见后面的附录。
一、导入依赖坐标
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>4.3.25.RELEASE</version>
- </dependency>
二、定义一个测试基类
这是利用了Java语言的继承性,可以简化未来具体用到的测试类,不用写重复的注解部分的代码。
- import org.junit.runner.RunWith;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
- /**
- * 配置spring和junit整合,junit启动时加载springIOC容器 spring-test,junit
- */
- @RunWith(SpringJUnit4ClassRunner.class)
- // 告诉junit spring配置文件的位置
- @ContextConfiguration({ "classpath:spring/spring-redis.xml","classpath:spring/spring-dao.xml",
- "classpath:spring/spring-service.xml" })
- public class BaseTest {
- }
三、编写具体的测试类,继承刚刚的测试基类:
通用Mapper插件是对mybatis框架在单表CRUD操作时支持性很好。此处的selectAll()和select(null)方法的查询效果是一样的,都是查询所有。
- public class SpringTkMapperTest extends BaseJunit4Test {
- @Autowired
- UserMapper userMapper;
- @Test
- public void testUserMapper() {
- //List<User> userList = userMapper.selectAll();
- List<User> userList = userMapper.select(null);
- System.out.println(userList);
- }
- }
附录:
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
- http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
- <!-- 加载配置文件,可以使用占位符替换功能 -->
- <context:property-placeholder
- location="classpath:properties/*.properties" ignore-unresolvable="true" />
- <!-- 定义dataSource数据源 -->
- <bean id="dataSource"
- class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
- <!-- 数据库驱动 -->
- <property name="driverClassName" value="${jdbc.driver}" />
- <!-- 相应驱动的jdbcUrl -->
- <property name="url" value="${jdbc.url}" />
- <!-- 数据库的用户名 -->
- <property name="username" value="${jdbc.username}" />
- <!-- 数据库的密码 -->
- <property name="password" value="${jdbc.password}" />
- <!-- 配置初始化大小、最小、最大 -->
- <property name="initialSize" value="5" />
- <property name="minIdle" value="5" />
- <property name="maxActive" value="10" />
- <!-- 配置从连接池获取连接等待超时的时间,单位是毫秒 -->
- <property name="maxWait" value="10000" />
- <!-- 配置间隔多久启动一次DestroyThread,对连接池内的连接才进行一次检测,单位是毫秒。
- 检测时:1.如果连接空闲并且超过minIdle以外的连接,如果空闲时间超过minEvictableIdleTimeMillis设置的值则直接物理关闭。
- 2.在minIdle以内的不处理。 -->
- <property name="timeBetweenEvictionRunsMillis" value="600000" />
- <!-- 配置一个连接在池中最大空闲时间,单位是毫秒 -->
- <property name="minEvictableIdleTimeMillis" value="300000" />
- <!-- 设置从连接池获取连接时是否检查连接有效性,true时,每次都检查;false时,不检查 -->
- <property name="testOnBorrow" value="false" />
- <!-- 设置往连接池归还连接时是否检查连接有效性,true时,每次都检查;false时,不检查 -->
- <property name="testOnReturn" value="false" />
- <!-- 设置从连接池获取连接时是否检查连接有效性,
- true时,如果连接空闲时间超过minEvictableIdleTimeMillis进行检查,否则不检查;
- false时,不检查 -->
- <property name="testWhileIdle" value="true" />
- </bean>
- <!-- 让spring管理sqlSessionFactory 使用mybatis和spring整合包中的 -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <!-- 数据库连接池 -->
- <property name="dataSource" ref="dataSource" />
- <!-- 加载mybatis的全局配置文件 -->
- <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
- <!-- 实体类别名包 -->
- <property name="typeAliasesPackage" value="cn.itcast.usermanage.pojo"/>
- <!-- 配置mapper.xml文件 -->
- <!-- <property name="mapperLocations" value="classpath*:mybatis/mappers/*Mapper.xml"/> -->
- <property name="plugins">
- <array>
- <bean class="com.github.pagehelper.PageInterceptor">
- <property name="properties">
- <value>
- helperDialect=mysql
- reasonable=true
- rowBoundsWithCount=true
- </value>
- </property>
- </bean>
- </array>
- </property>
- </bean>
- <!-- 配置扫描接口包,加载通用mapper代理对象 -->
- <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
- <!-- 扫描多个包地址 -->
- <property name="basePackage" value="cn.itcast.usermanage.mapper" />
- </bean>
- </beans>
- <!-- 集中定义依赖版本号,参考了SpringBboot启动器的版本 -->
- <properties>
- <junit.version>4.12</junit.version>
- <slf4j.version>1.7.28</slf4j.version>
- <spring.version>4.3.25.RELEASE</spring.version>
- <mybatis.version>3.4.6</mybatis.version>
- <mybatis.spring.version>1.3.2</mybatis.spring.version>
- <mybatis.paginator.version>1.2.17</mybatis.paginator.version>
- <mapper.version>4.1.5</mapper.version>
- <pagehelper.version>5.1.10</pagehelper.version>
- <jsqlparser.version>2.0</jsqlparser.version>
- <mysql.version>5.1.47</mysql.version>
- <druid.version>1.1.15</druid.version>
- <jackson.version>2.9.9.3</jackson.version>
- <jstl.version>1.2</jstl.version>
- <servlet-api.version>3.1.0</servlet-api.version>
- <jsp-api.version>2.2</jsp-api.version>
- <httpclient.version>4.5.10</httpclient.version>
- <joda-time.version>2.9.9</joda-time.version>
- <commons-lang3.version>3.9</commons-lang3.version>
- <commons-io.version>2.5</commons-io.version>
- <commons-net.version>3.6</commons-net.version>
- <commons-fileupload.version>1.4</commons-fileupload.version>
- <jedis.version>3.1.0</jedis.version>
- <solrj.version>7.7.2</solrj.version>
- </properties>
=======================================================
参考资料:https://www.cnblogs.com/gkaigk1987/articles/5367569.html
Spring Test+JUnit4整合使用测试ZZJ_淘淘商城项目:day01(RESTful Web Service)的更多相关文章
- Apache CXF实现Web Service(3)——Tomcat容器和不借助Spring的普通Servlet实现JAX-RS(RESTful) web service
起步 参照这一系列的另外一篇文章: Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service 首先 ...
- 构建一个基于 Spring 的 RESTful Web Service
本文详细介绍了基于Spring创建一个“hello world” RESTful web service工程的步骤. 目标 构建一个service,接收如下HTTP GET请求: http://loc ...
- Building a RESTful Web Service Using Spring Boot In Eclipse
一.构建restful web service 创建Maven的java web工程,maven的pom文件加入依赖包 创建包hello Greeting.java package hello; pu ...
- (转)接口自动化测试 – Java+TestNG 测试 Restful Web Service
本文主要介绍如何用Java针对Restful web service 做接口自动化测试(数据驱动),相比UI自动化,接口自动化稳定性可靠性高,实施难易程度低,做自动化性价比高.所用到的工具或类库有 T ...
- Apache CXF实现Web Service(4)——Tomcat容器和Spring实现JAX-RS(RESTful) web service
准备 我们仍然使用 Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service 中的代码作为基础,并 ...
- Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service
实现目标 http://localhost:9000/rs/roomservice 为入口, http://localhost:9000/rs/roomservice/room为房间列表, http: ...
- 【转】Spring 4.x实现Restful web service
http://my.oschina.net/yuyidi/blog/352909 首先我们还是跟之前一样,创建一个maven项目,不过因为Spring Restful web service是基于Sp ...
- 用Spring Tools Suite(STS)开始一个RESTful Web Service
spring.io官方提供的例子Building a RESTful Web Service提供了用Maven.Gradle.STS构建一个RESTFul Web Service,实际上采用STS构建 ...
- [翻译]Spring MVC RESTFul Web Service CRUD 例子
Spring MVC RESTFul Web Service CRUD 例子 本文主要翻译自:http://memorynotfound.com/spring-mvc-restful-web-serv ...
随机推荐
- Sublime Text与LaTeX的结合
1.通过Command Palette安装LaTeXTools 2.通过Command Palette查找LaTeXTools: Reconfigure and migrate settings,回车
- 修改虚拟机ip
在命令行里输入:more /etc/sysconfig/network-scripts/ifcfg-eth0 (注意more后要空一格,还有eth0,最后是数字零). 然后再输入:ifconfig ...
- 一本通1166 求f(x,n)
[题目描述] 已知 计算x=4.2,n=1以及x=2.5,n=15时f的值. [输入] 输入x和n. [输出] 函数值,保留两位小数. [输入样例] 4.2 10 [输出样例] 3.68 1.看见这种 ...
- 四、SAP中转义字符的使用和不同数据类型所占长度不同
一.代码如下: 效果如下,注意看,数字类型占用了10个位置:
- 剑指offer第40题
package com.yan.offer; /** * 题目描述: * * 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. * * @author Ya ...
- mysql插入文本文档及读取
1.把本地的一个文件插入到数据库中,数据库字段用text保存 public static void main(String[] args) { PropKit.use(“pro.txt”);Druid ...
- [软件] Omnigraffle
一个商业软件, mac下画画图, 还挺好用的. 网上可以找到可用的注册码 https://blog.csdn.net/glw0223/article/details/90736751
- 9.scrapy pycharm调试小技巧,请求一次,下次直接调试,不必每次都启动整个爬虫,重新请求一整遍
pycharm调试技巧:调试时,请求一次,下次直接调试,不必每次都启动整个爬虫,重新请求一整遍 [用法]cmd命令运行:scrapy shell 网址 第一步,cmd进行一次请求: scrapy sh ...
- 4)栈和队列-->受限线性表
栈和队列叫 受限线性表 只不过他们插入和删除的位置 相对于之前的线性表有了限制 所以叫受限线性表 1)栈-->就是先进后出 2)队列-->先进先出 3)循环链表框图: 4)队列
- TensorFlow(一)
一.Hello World 1.只安装CPU版,TensorFlow1.14.0版本代码 # import tensorflow as tf import tensorflow.compat.v1 a ...