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"));
}
}

推荐:Spring Boot & Cloud 最强技术教程

Spring Boot 单元测试详解+实战教程的更多相关文章

  1. Spring Boot 配置文件详解

    Spring Boot配置文件详解 Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件.他们的作用都是修改Spring Boot自动配置的默认值.相对于prop ...

  2. Spring Boot异常处理详解

    在Spring MVC异常处理详解中,介绍了Spring MVC的异常处理体系,本文将讲解在此基础上Spring Boot为我们做了哪些工作.下图列出了Spring Boot中跟MVC异常处理相关的类 ...

  3. (转) SpringBoot非官方教程 | 第二篇:Spring Boot配置文件详解

    springboot采纳了建立生产就绪spring应用程序的观点. Spring Boot优先于配置的惯例,旨在让您尽快启动和运行.在一般情况下,我们不需要做太多的配置就能够让spring boot正 ...

  4. SpringBoot非官方教程 | 第二篇:Spring Boot配置文件详解

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot2-config-file/ 本文出自方志朋的博客 ...

  5. Spring Boot 注解详解

    一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...

  6. Spring Boot配置文件详解-ConfigurationProperties和Value优缺点-(转)好文

    文章转自 http://www.cnblogs.com/itdragon/p/8686554.html Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件.他们 ...

  7. Spring Boot☞ 配置文件详解:自定义属性、随机数、多环境配置等

    自定义属性与加载 我们在使用Spring Boot的时候,通常也需要定义一些自己使用的属性,我们可以如下方式直接定义: application-dev.yml com.didispace.blog: ...

  8. Spring Boot配置文件详解:自定义属性、随机数、多环境配置

    自定义属性与加载 我们在使用Spring Boot的时候,通常也需要定义一些自己使用的属性,我们可以如下方式直接定义: application-dev.yml com.didispace.blog: ...

  9. Spring Boot (九): 微服务应用监控 Spring Boot Actuator 详解

    1. 引言 在当前的微服务架构方式下,我们会有很多的服务部署在不同的机器上,相互是通过服务调用的方式进行交互,一个完整的业务流程中间会经过很多个微服务的处理和传递,那么,如何能知道每个服务的健康状况就 ...

随机推荐

  1. JAVA远程调试

    1.远程端启动必须添加jvm参数 -Xdebug -Xrunjdwp:transport=dt_socket,suspend=n,server=y,address=${debug_port} 其中de ...

  2. C++11 constexpr常量表达式

    常量表达式函数 要求: 函数体内只有单一的return返回语句 例如: constexpr int data() { const int i=1; //含有除了return以外的语句 return i ...

  3. python基础 ---- 使用pyCharm 调试

    debug -- 为了分析程序的异常 单步调试 1.设置断点 2.debug.启动  3.监控变量

  4. spring mvc重定向

    spring mvc重定向有三种方法. 1.return new ModelAndView("redirect:/toUrl"); 其中/toUrlt是你要重定向的url. 2.r ...

  5. spring多线程

    Spring4.x高级话题(二):多线程 一. 点睛 Spring通过任务执行器(TaskExecutor)来实现多线程和并发编程.使用ThreadPoolTaskExecutor可实现一个基于线程池 ...

  6. Quartz.Net进阶之三:SimpleTrigger详述

    以前都是将所有的内容放在一篇文章里,就会导致文章很长,对于学习的人来说,有时候这也是一个障碍.所以,以后我的写作习惯,我就会把我写的文章缩短,但是内容不会少,内容更集中.这样,学习起来也不会很累,很容 ...

  7. 20165213Java第二次实验

    实验二Java面向对象程序设计 实验1 实验目的与要求: 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST 完成单元测试的学习 提交 ...

  8. 借助Algorithmia网站API:用AI给黑白照片上色,复现记忆中的旧时光

    先看DEMOhttps://demos.algorithmia.com/colorize-photos/ 了解ColorfulImageColorizationhttps://algorithmia. ...

  9. 201771010142 张燕《面向对象程序设计(java)》第一周学习总结

    201771010142 张燕<面向对象程序设计(java)>第一周学习总结 第一部分:课程准备部分 填写课程学习 平台注册账号, 平台名称 注册账号 博客园:www.cnblogs.co ...

  10. eclipse配置servlet错误

    可能是因为你的web.xml里的<url>映射的名字和servlet相同