SpringBoot 添加junit单元测试+Spring Boot 的测试类库
第一部分、SpringBoot 添加junit单元测试
SpringBoot使用junit非常简单,我们来看一下,首先说明,这里使用的是springboot2.0.4的版本
一.pom.xml文件开启springboot测试包
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
</dependency>
二.创建一个测试类
1.单文件测试,直接添加@Test注解即可,如图:
2.MVC形式调用
测试类,需要加上两个注解
@RunWith(SpringRunner.class)@SpringBootTest(classes={App.class})
其中App.class是主程序入口类,即springboot的启动类
package com.qfx.test;
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 com.qfx.system.App;
import com.qfx.system.service.SysUserService;
@RunWith(SpringRunner.class)
@SpringBootTest(classes={App.class})
public class JunitTest {
<span class="hljs-meta">@Autowired</span>
SysUserService sysUserService;<span class="hljs-meta">@Test</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">printSysUserInfo</span><span class="hljs-params">()</span></span>{String userStr = sysUserService.getSysUserAll();System.out.println(userStr);
}
}
进行测试的时候会通过App.class来启动springboot,我们来看一下效果,如图:
至于对Controller的测试可以直接启动项目,通过浏览器来发送请求测试了,如果实在想使用junit来测试的话,可以使用来MockMvc进行
第二部分、Spring Boot 的测试类库
Spring Boot 提供了许多实用工具和注解来帮助测试应用程序,主要包括以下两个模块。
spring-boot-test:支持测试的核心内容。
spring-boot-test-autoconfigure:支持测试的自动化配置。
开发进行只要使用 spring-boot-starter-test
启动器就能引入这些 Spring Boot 测试模块,还能引入一些像 JUnit, AssertJ, Hamcrest
及其他一些有用的类库,具体如下所示。
- JUnit:Java 应用程序单元测试标准类库。
- Spring Test & Spring Boot Test:Spring Boot 应用程序功能集成化测试支持。
- AssertJ:一个轻量级的断言类库。
- Hamcrest:一个对象匹配器类库。
- Mockito:一个Java Mock测试框架,默认支付 1.x,可以修改为 2.x。
- JSONassert:一个用于JSON的断言库。
- JsonPath:一个JSON操作类库。
下面是 Maven 的依赖关系图。
以上这些都是 Spring Boot 提供的一些比较常用的测试类库,如果上面的还不能满足你的需要,你也可以随意添加其他的以上没有的类库。
测试 Spring Boot 应用程序
添加 Maven 依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><version>1.5.10.RELEASE</version><scope>test</scope>
</dependency>
1、 要让一个普通类变成一个单元测试类只需要在类名上加入 @SpringBootTest 和 @RunWith(SpringRunner.class) 两个注释即可。
2、 在测试方法上加上 @Test 注释。
如果测试需要做 REST 调用,可以 @Autowire 一个 TestRestTemplate。
@RunWith(SpringRunner.class)
@SpringBootTest
public class BBTestAA {
@Autowired
private TestRestTemplate testRestTemplate;
@Test
public void testDemo() {
…
}
}
GET请求测试
@Test
public void get() throws Exception {Map<String,String> multiValueMap = new HashMap<>();multiValueMap.put("username","Java技术栈");ActResult result = testRestTemplate.getForObject("/test/getUser?username={username}",ActResult.class,multiValueMap);Assert.assertEquals(result.getCode(),0);
}
POST请求测试
@Test
public void post() throws Exception {MultiValueMap multiValueMap = new LinkedMultiValueMap();multiValueMap.add("username","Java技术栈");ActResult result = testRestTemplate.postForObject("/test/post",multiValueMap,ActResult.class);Assert.assertEquals(result.getCode(),0);
}
文件上传测试
@Test
public void upload() throws Exception {Resource resource = new FileSystemResource("/home/javastack/test.jar");MultiValueMap multiValueMap = new LinkedMultiValueMap();multiValueMap.add("username","Java技术栈");multiValueMap.add("files",resource);ActResult result = testRestTemplate.postForObject("/test/upload",multiValueMap,ActResult.class);Assert.assertEquals(result.getCode(),0);
}
文件下载测试
@Test
public void download() throws Exception {HttpHeaders headers = new HttpHeaders();headers.set("token","javastack");HttpEntity formEntity = new HttpEntity(headers);String[] urlVariables = new String[]{"admin"};ResponseEntity<byte[]> response = testRestTemplate.exchange("/test/download?username={1}",HttpMethod.GET,formEntity,byte[].class,urlVariables);if (response.getStatusCode() == HttpStatus.OK) {Files.write(response.getBody(),new File("/home/javastack/test.jar"));}
}
以上第二部分来源于:https://www.cnblogs.com/javastack/p/9150408.html
SpringBoot 添加junit单元测试+Spring Boot 的测试类库相关推荐
- SpringBoot入门系列: Spring Boot的测试
Spring Boot的测试,和普通项目的测试类同,可以继续使用我们熟悉的测试工具.当然,这里的测试,主要还是后台代码的测试. 主要需要注意的地方仅有三点: 1.依赖包的引入:pom.xml中仅依赖s ...
- Spring boot(十二):Spring boot 如何测试、打包、部署
博文引用:springboot(十二):springboot如何测试打包部署 开发阶段 单元测试 Spring boot对单元测试的支持已经很完善了. 1 在pom包中添加Spring-boot-st ...
- 极其简单的 使用IDEA 中 实现springboot 热部署 (spring boot devtools版)
添加配置pom.xml配置 第一步:添加springboot的配置文件 首先我先贴出我的配置 添加依赖包 <!-- spring boot devtools 依赖包. --><dep ...
- Junit单元测试不支持多线程测试问题全解析
一.背景 今天@段段提出了一个很好的问题,她发现单元测试时如果开多个线程,主线程运行结束就结束了,并不会等待子线程结束. 如果用main方法就没问题,技术群里展开了激烈的讨论. 本文将"复现 ...
- springboot毕设项目基于Spring Boot的智慧天气管理系统84z99(java+VUE+Mybatis+Maven+Mysql)
springboot毕设项目基于Spring Boot的智慧天气管理系统84z99(java+VUE+Mybatis+Maven+Mysql) 项目运行 环境配置: Jdk1.8 + Tomcat8. ...
- boot spring test 文档_SpringBoot入门十,添加junit单元测试
SpringBoot使用junit非常简单,我们来看一下,首先说明,这里使用的是springboot2.0.4的版本 一.pom.xml文件开启springboot测试包 org.springfram ...
- springboot test_精益求精!Spring Boot 知识点全面回顾,带你重新细读源码!
作者:cyd_0619 原文:https://blog.csdn.net/cyd_0619 约定优于配置 Build Anything with Spring Boot:Spring Boot is ...
- (转)Spring Boot(十二):Spring Boot 如何测试打包部署
http://www.ityouknow.com/springboot/2017/05/09/spring-boot-deploy.html 有很多网友会时不时的问我, Spring Boot 项目如 ...
- springboot+mysql+小程序+spring boot美食教程小程序 毕业设计源码190900
Springboot美食教程小程序的设计与实现 摘 要 本文设计了一种基于微信小程序的美食教程小程序,系统为人们提供了方便快捷.即用即搜的美食教程服务,包括美食资讯.美食话题.注册登录等,用户能够方便 ...
- springboot使用junit单元测试是发生报错 Field taskUtils in com.xxx.xxx.xxxx.xxx required a bean of type 'xxx.xxx.
使用junit单元测试mybatis时发生如下错误 Error starting ApplicationContext. To display the conditions report re-run ...
最新文章
- SDUTOJ 1293 乘积最大的分解(数论)
- zabbix 自动注册发现
- 论坛答疑SQL(二)
- linux创建的kvm无法运行,使用virt-manager运行虚拟机的方法(创建第一个虚拟机)...
- CSS的预编译——less语言基本语法教程(入门)
- S3C2440PWM 定时器
- wps解密excel表格xlsx文件,excel表格xlsx权限密码多少?
- pcsx2运行ps1_PS2模拟器PSX2设置及使用教程.doc
- 英文科技论文写作与学术报告Lecture 2习题答案
- 分形之皇冠(Crown)
- windows找不到文件regedit_exe和taskmgr_exe的解决方法
- 猜疑链,区块链,微信群有什么关系?
- 拍乐云创始人CEO赵加雨:深耕18载,打造全景式音视频服务
- 【新知实验室】腾讯云TRTC服务体验
- 美团一面究竟有多难?
- ftp、go-fastdfs、HelpManual、redis、git、ngnix
- 南卡蓝牙耳机和JBL蓝牙耳机哪个更值得买?音质最好的蓝牙耳机测评
- 操作系统实验一:线程的创建与撤销
- 服务器安装node全教程
- 中职教资证计算机应用,中职计算机教师资格证只能教中职学校的吗