SpringBoot单元测试
SpringBoot
一、Service层Junit单元测试
需要的jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
Springboot 1.3的版本与1.4的版本稍有不同
1.3及以下版本
package com.suning.epp.fmasosweb.service.impl; import com.suning.epp.fmasosweb.FmasosWebApplication;
import com.suning.epp.fmasosweb.result.RankGenreResult;
import com.suning.epp.fmasosweb.service.intf.CommentService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* 〈一句话功能简述〉
* 〈功能详细描述〉
*
* @author 17090889
* @see [相关类/方法](可选)
* @since [产品/模块版本] (可选)
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = FmasosWebApplication.class)
@WebAppConfiguration
public class CommentServiceImplTest { @Autowired
private CommentService commentService; @Test
public void queryAppRankGenreResultTest() {
Map<String, String> param = new HashMap<>();
List<RankGenreResult> rankGenreResultList = commentService.queryAppRankGenreResult(param);
System.out.println(rankGenreResultList);
} }
1.4及以上版本
@SpringApplicationConfiguration 注解标记为过时了
提供了注解@SpringBootTest
使用SpringRunner 替代 SpringJUnit4ClassRunner
package com.suning.epp.fmasosweb.service.impl; import com.suning.epp.fmasosweb.result.RankGenreResult;
import com.suning.epp.fmasosweb.service.intf.CommentService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* 〈一句话功能简述〉
* 〈功能详细描述〉
*
* @author 17090889
* @see [相关类/方法](可选)
* @since [产品/模块版本] (可选)
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class CommentServiceImplTest { @Autowired
private CommentService commentService; @Test
public void queryAppRankGenreResultTest() {
Map<String, String> param = new HashMap<>();
List<RankGenreResult> rankGenreResultList = commentService.queryAppRankGenreResult(param);
System.out.println(rankGenreResultList);
} }
二、Controller层Mock测试
1.3及以下版本
package com.suning.epp.fmasosadmin.mapper; import com.suning.epp.fmasos.FmasosApplication;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext; /**
* 〈一句话功能简述〉
* 〈功能详细描述〉
*
* @author 17090889
* @see [相关类/方法](可选)
* @since [产品/模块版本] (可选)
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = FmasosApplication.class)
@WebAppConfiguration
@Transactional
public class ProcessorServiceTest { // @Autowired
// @Qualifier("commentProcessorServiceImpl")
// private CommentProcessorService commentProcessorServiceImpl; @Autowired
private WebApplicationContext webApplicationContext; private MockMvc mockMvc; @Before
public void setUp() throws Exception {
//构造MockMvc
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); } @Test
public void spiderRun() throws Exception {
String url = "/comment/spiderRun2";
mockMvc.perform(MockMvcRequestBuilders.get(url));
} }
1.4及以上版本
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import static org.junit.Assert.*; @RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class CommentControllerTest {
@Autowired
private MockMvc mvc; @Test
public void spiderRun() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/spiderRun"))
.andExpect(MockMvcResultMatchers.status().isOk());
//.andExpect(MockMvcResultMatchers.content().string("365")); //测试接口返回内容
} }
Spring
1、需要在test/resources下新建spring配置文件,扫描注入测试需要的所有bean及依赖bean
/**
* @author yangyongjie
* @date 2020/2/26
* @desc
*/
@RunWith(SpringJUnit4ClassRunner.class) // 启动 Spring 对测试类的支持
@ContextConfiguration("classpath:spring-*.xml") // 指定 Spring 配置文件或者配置类的位置,classpath路径为test/resources
public class AutoRenewCheckTaskTest { @Autowired
private AutoRenewCheckTask autoRenewCheckTask; @Test
public void executeTest(){
autoRenewCheckTask.execute();
} }
2、不在test/resources下新建spring配置文件也可,使用main/resources 下的Spring配置文件,此时需要使用 @ContextConfiguration 注解的 locations 属性指定配置文件在计算机上的绝对路径,如:
@RunWith(SpringJUnit4ClassRunner.class) // 启动 Spring 对测试类的支持
@ContextConfiguration(locations = {"file:D:\\IdeaProjects\\taskModuleOptimize\\bssadmin-task\\src\\main\\webapp\\WEB-INF\\spring\\spring-*.xml"}) // 指定 Spring 配置文件或者配置类的位置
public class AutoRenewCheckTaskTest { @Autowired
private AutoRenewCheckTask autoRenewCheckTask; @Test
public void executeTest(){
autoRenewCheckTask.execute();
} }
end
SpringBoot单元测试的更多相关文章
- Springboot单元测试Junit深度实践
Springboot单元测试Junit深度实践 前言 单元测试的好处估计大家也都知道了,但是大家可以发现在国内IT公司中真正推行单测的很少很少,一些大厂大部分也只是在核心产品推广单测来保障质量,今天这 ...
- springmvc,springboot单元测试配置
1. springmvc单元测试配置 <dependency> <groupId>junit</groupId> <artifactId>junit&l ...
- SpringBoot单元测试中的事务和Session
1.Springboot中使用junit编写单元测试,并且测试结果不影响数据库. 2.
- springboot(十二):springboot单元测试、打包部署
单元测试 1.在pom包中添加spring-boot-starter-test包引用 <dependency> <groupId>org.springframework.boo ...
- springboot系列三、springboot 单元测试、配置访问路径、多个配置文件和多环境配置,项目打包发布
一.单元测试 生成的demo里面包含spring-boot-starter-test :测试模块,包括JUnit.Hamcrest.Mockito,没有的手动加上. <dependency> ...
- Springboot单元测试(MockBean||SpyBean)
转载:https://blog.csdn.net/maiyikai/article/details/78483423 本来要写springboot集成netty实现的,但是想起来单元测试没总结,那就趁 ...
- 五、springboot单元测试
1.为什么要写测试用例 1. 可以避免测试点的遗漏,为了更好的进行测试,可以提高测试效率 2. 可以自动测试,可以在项目打包前进行测试校验 3. 可以及时发现因为修改代码导致新的问题的出现,并及时解决 ...
- springBoot单元测试-基础单元测试
1)在pom文件中加入junit支持 <!-- spring-boot-starter-test 单元测试 --> <dependency> <groupId>or ...
- 【使用篇二】SpringBoot单元测试(10)
SpringCloud单元测试:https://www.cnblogs.com/myitnews/p/11796321.html 1. 创建项目Maven Project,修改pom.xml < ...
随机推荐
- Flask开发微电影网站(八)
1.后台管理之电影预告管理 1.1 定义电影预告表单 在app的admin目录的forms.py文件中,定义电影预告表单 # 预告表单 class PreviewForm(FlaskForm): ti ...
- java----Maven
下载地址 http://maven.apache.org/download.cgi 介绍 bin:运行脚本 windows 输入mvn可以运行这些脚本 boot:包含一个类加载器的框架,maven使用 ...
- 论文阅读笔记四十:Deformable ConvNets v2: More Deformable, Better Results(CVPR2018)
论文源址:https://arxiv.org/abs/1811.11168 摘要 可变形卷积的一个亮点是对于不同几何变化的物体具有适应性.但也存在一些问题,虽然相比传统的卷积网络,其神经网络的空间形状 ...
- Java组合模式
定义:将对象组合成树形结构以表示 部分--整体的层次结构 组合模式使客户端对单个对象和组合对象保持一致的方式处理 类型:结构型 优点: 1.清楚地定义分层次的复杂对象,表示对象的全部去或部分层次 2 ...
- ASP.NET 多环境下配置文件web.config的灵活配置
调试,发布Asp.net程序的时候,开发环境和发布环境的Web.Config往往不同,比如connectionstring等.如果常常有调试,发布的需求,就需要常常修改web.config文件,这往往 ...
- Python学习计划
---恢复内容开始--- Python学习计划 https://edu.csdn.net/topic/python2?utm_source=blog4 匠人之心,成就真正Python全栈工程师 ...
- 解决Mysql命令行输入密码闪退问题
输入密码闪退是因为后台Mysql服务没有启动. 解决办法:我的电脑,右键管理,服务,查看服务里面Mysql是否在运行.如果没有在运行那么可以右键启动,最好属性中设置为自动启动.
- Class--2019-04-14
获取class对象,有三种方法: 1.通过类名.class直接访问 Class c = Integer.class; 2.通过Class.forName(类名)函数获取 Class c = Class ...
- Python 地点转化为经纬度
1.geopy包下载地点https://pypi.python.org/pypi/geopy 2.安装步骤 运行cmd,切换到D:/python/geopy-1.11.0/目录下 ...
- 使用 Java 将多个文件压缩成一个压缩文件
使用 Java 将多个文件压缩成一个压缩文件 一.内容 ①使用 Java 将多个文件打包压缩成一个压缩文件: ②主要使用 java.io 下的类 二.源代码:ZipMultiFile.java pac ...