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 < ...
随机推荐
- 浅析Memcache和Redis
想必开发的小伙伴们对Memcache和Redis都不陌生吧,最近正好在整理它们,于是就写一下博客吧!一方面是分享,另一方面便于自己查找. 首先,来说说Memcache和Redis是什么? 说得简单一点 ...
- mac svn无法保存密码,JetBrains IDE(WebStrom、IntelliJ IDEA) 反复提示输入密码
一.vim ~/.subversion/config用vim修改以下四个地方store-passwords = yesstore-plaintext-passwords = yesstore-ssl- ...
- Spark SQL大数据处理并写入Elasticsearch
SparkSQL(Spark用于处理结构化数据的模块) 通过SparkSQL导入的数据可以来自MySQL数据库.Json数据.Csv数据等,通过load这些数据可以对其做一系列计算 下面通过程序代码来 ...
- spark算子
1.map 一条一条读取 def map(): Unit ={ val list = List("张无忌", "赵敏", "周芷若") va ...
- fiddler基本功能介绍
一.几种主流的抓包工具的对比: Wireshark:通用的抓包工具,抓取信息量庞大,详细.通常需要过滤才可容易得到有用信息.如果只抓http请求个人认为有点大材小用. Firebug.httpWatc ...
- 自定义PlantUML和C4 Model样式
什么是PlantUml PlantUml是一个支持快速绘制的开源项目.其定义了一套完整的语言用于实现UML关系图的描述.并基于强大的graphviz图形渲染库进行UML图的生成.绘制的UML图还可以导 ...
- Typescript高级类型与泛型难点详解
最近做的TS分享,到了高级类型这一块.通过琢磨和实验还是挖掘出了一些深层的东西,在此处做一下记录,也分享给各位热爱前端的小伙伴. 其实在学习TS之前就要明确以下几点: 1. typescrip ...
- pandas 必背函数操作
1.五个常用属性 index,columns,shape,values,dtypes2.常用函数:set_index,reset_index,del df['column_name'],pd.read ...
- python ironicclient源码分析
ironicclient是一个cli工具,用来和用户交互的. 首先写一个简单的例子,获取ironic所有的node节点: from ironicclient import client if __na ...
- Hadoop双namenode配置搭建(HA)
配置双namenode的目的就是为了防错,防止一个namenode挂掉数据丢失,具体原理本文不详细讲解,这里只说明具体的安装过程. Hadoop HA的搭建是基于Zookeeper的,关于Zookee ...