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 < ...
随机推荐
- 用JS实现实时显示系统时间
废话我就不多说了,直接上图和代码了 效果图: 代码视图: 下面为大家附上代码,复制即可用: <!DOCTYPE html> <html lang="en"> ...
- django 实战篇之视图层
视图层(views.py) django必会三板斧 HttpResponse >>> 返回字符串 render >>> 支持模板语法,渲染页面,并返回给前端 red ...
- Selenium+Python ---- 免登录
1.免登录在进行测试的过程中难免会遇到登录的情况,给测试工作添加了工作量,本文仅提供一些思路供参考解决方式:手动请求中添加cookies.火狐的profile文件记录信息实现.人工介入.万能验证码.去 ...
- Ethereum(1)—— 基本介绍
1 环境安装 安装Ethereum 协议的Go语言的最新实现. git clone https://github.com/ethereum/go-ethereum.git make all cd bu ...
- vue v-cloak知识点
1.使用 v-cloak 属性可以解决插值表达式闪烁问题; 2.v-text默认是没有闪烁的问题,同时会覆盖元素中原本的内容,但是v-cloak只会替换 自己的这个占位符,不会替换所有的字符 ...
- C++ log4cplus 类库的封装
对 log4cplus 库的封装,修改自网路 LogUtils.h /* * LogUtils.h * * Created on: 2018年8月9日 * Author: oftenlin */ #i ...
- Python 小知识 杂七杂八 随手记
1.assert 断言语句 例1: print ‘11111111111’ assert 1==2 print ‘22222222’ 如果没有 assert 程序会输出 ‘1111111111 ...
- css 元素溢出
css元素溢出: 当子元素的尺寸超过父元素的尺寸时,需要设置父元素显示溢出的子元素的方式,设置的方法是通过overflow属性来设置. overflow的设置项: (1)visible 默认值 内容不 ...
- __x__(16)0906第三天__层叠样式表CSS简介
层叠样式表CSS Cascading Style Sheets 用来为网页创建样式表,通过样式表对网页进行装饰. 所谓层叠,就是将网页想象成一层一层的结构,层次高的将覆盖层次低的. CSS可以为网页的 ...
- ECMA Script 6_对象的扩展
对象 1. ES6 允许直接写入变量和函数,作为对象的属性和方法 const foo = 'bar'; /*****************属性的优化********************/ con ...