Spring Boot的TestRestTemplate使用
文章目录
Spring Boot的TestRestTemplate使用
TestRestTemplate和RestTemplate很类似,不过它是专门用在测试环境中的,本文我们将会讲述TestRestTemplate的一些常用方法。
如果我们在测试环境中使用@SpringBootTest,则可以直接使用TestRestTemplate。
添加maven依赖
要使用TestRestTemplate,我们需要首先添加如下的maven依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
TestRestTemplate VS RestTemplate
TestRestTemplate和RestTemplate的功能很类似,都可以用来和HTTP API进行交互。实际上TestRestTemplate就是RestTemplate的封装。 我们看下TestRestTemplate的代码:
public class TestRestTemplate {
private final RestTemplateBuilder builder;
private final HttpClientOption[] httpClientOptions;
private final RestTemplate restTemplate;
...
public void setUriTemplateHandler(UriTemplateHandler handler) {
this.restTemplate.setUriTemplateHandler(handler);
}
...
以setUriTemplateHandler为例,我们看到实际上TestRestTemplate调用了restTemplate里面的具体方法。
我们看一下TestRestTemplate基本的使用:
@Test
public void testGet (){
TestRestTemplate testRestTemplate = new TestRestTemplate();
ResponseEntity<String> response = testRestTemplate.
getForEntity(FOO_RESOURCE_URL + "/1", String.class);
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
}
使用Basic Auth Credentials
TestRestTemplate封装了基本的Auth Credentials,我们可以这样使用:
TestRestTemplate testRestTemplate
= new TestRestTemplate("user", "passwd");
ResponseEntity<String> response = testRestTemplate.
getForEntity(URL_SECURED_BY_AUTHENTICATION, String.class);
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
使用HttpClientOption
HttpClientOption提供了如下几个选项:ENABLE_COOKIES, ENABLE_REDIRECTS, 和 SSL。
我们看下TestRestTemplate怎么使用:
TestRestTemplate testRestTemplate = new TestRestTemplate("user",
"passwd", TestRestTemplate.HttpClientOption.ENABLE_COOKIES);
ResponseEntity<String> response = testRestTemplate.
getForEntity(URL_SECURED_BY_AUTHENTICATION, String.class);
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
如果我们不需要认证,则可以这样使用:
TestRestTemplate(TestRestTemplate.HttpClientOption.ENABLE_COOKIES)
我们也可以在创建TestRestTemplate之后添加认证:
TestRestTemplate testRestTemplate = new TestRestTemplate();
ResponseEntity<String> response = testRestTemplate.withBasicAuth(
"user", "passwd").getForEntity(URL_SECURED_BY_AUTHENTICATION,
String.class);
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
使用RestTemplateBuilder
RestTemplateBuilder为我们提供了自定义RestTemplate的机会,我们可以使用它来对RestTemplate进行封装:
RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder();
restTemplateBuilder.configure(restTemplate);
TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplateBuilder);
ResponseEntity<String> response = testRestTemplate.getForEntity(
FOO_RESOURCE_URL + "/1", String.class);
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
本文的例子可以参考https://github.com/ddean2009/learn-springboot2/tree/master/springboot-testRestTemplate
更多教程请参考 flydean的博客
Spring Boot的TestRestTemplate使用的更多相关文章
- spring boot使用TestRestTemplate集成测试 RESTful 接口
这篇文章没什么技术含量,只是单纯的记录一下如何用TestRestTemplate访问受security保护的api,供以后查阅. @Slf4j @RunWith(SpringRunner.class) ...
- 玩转spring boot——快速开始
开发环境: IED环境:Eclipse JDK版本:1.8 maven版本:3.3.9 一.创建一个spring boot的mcv web应用程序 打开Eclipse,新建Maven项目 选择quic ...
- spring boot单元测试(转)
Junit这种老技术,现在又拿出来说,不为别的,某种程度上来说,更是为了要说明它在项目中的重要性.凭本人的感觉和经验来说,在项目中完全按标准都写Junit用例覆盖大部分业务代码的,应该不会超过一半. ...
- 40. Testing Prev Part IV. Spring Boot features
40. Testing Spring Boot provides a number of utilities and annotations to help when testing your app ...
- (转)Spring Boot Junit单元测试
场景:在项目开发中要测试springboot工程中一个几个dao和service的功能是否正常,初期是在web工程中进行要素的录入,工作量太大.使用该单元测试大大减小了工作强度. Junit这种老技术 ...
- Spring Boot单元测试(Mock)
Spring Boot单元测试(Mock) Java个人学习心得 2017-08-12 16:07 Mock 单元测试的重要性就不多说了,我这边的工程一般都是Spring Boot+Mybatis(详 ...
- Spring Boot 1.4测试的改进
对Pivotal团队来说,工作上的好事情是他们拥有一个被叫做Pivotal Labs的灵活发展部门,拥有Labs团队的Lean 和 XP程序设计方法学的强大支持,例如结对编程和测试驱动开发.他们对于测 ...
- Spring Boot 1.4测试的简单理解
首先maven要引入spring-boot-starter-test这个包. 先看一段代码 @RunWith(SpringRunner.class) @SpringBootTest(webEnviro ...
- 详细介绍Spring Boot 2.0的那些新特性与增强
以Java 8 为基准 Spring Boot 2.0 要求Java 版本必须8以上, Java 6 和 7 不再支持. 内嵌容器包结构调整 为了支持reactive使用场景,内嵌的容器包结构被重构了 ...
随机推荐
- Three.js制作一个基本的飞行3D场景【源代码】
原文链接:https://tympanus.net/codrops/2016/04/26/the-aviator-animating-basic-3d-scene-threejs/ 源代码下载链接:h ...
- wsl中配置SML环境
配置SML/NJ #安装 sudo apt install smlnj #但是wsl不支持32位程序,所以需要下面配置 sudo dpkg --add-architecture i386 sudo a ...
- RedHat7.4配置yum网络源
本次RedHat版本为:Red Hat Enterprise Linux Server release 7.4 (Maipo). 将RedHat7.4的yum源替换为免费的CentOS对应版本yum源 ...
- Shell基础应用
Shell基础应用 案例1:Shell基础应用 案例2:简单Shell脚本的 ...
- VMwareWorkstation如何设置共享文件夹
首先需要安装VMware Tools 这个嘛,应该是需要安装的,之前没有安装好像就没有设置成功. 没有安装的参考如何安装VMware Tools 然后在虚拟机设置里面设置共享路径 右键虚拟机名称,打开 ...
- 操作系统-2-存储管理之LRU页面置换算法(LeetCode146)
LRU缓存机制 题目:运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制. 它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - ...
- 2017蓝桥杯取位数(C++B组)
题目: 标题:取数位求1个整数的第k位数字有很多种方法.以下的方法就是一种.// 求x用10进制表示时的数位长度 int len(int x){ if(x<10) return 1; retur ...
- Matlab入门(一)
1.常用命令 cd 显示或改变当前工作目录 load 加载指定文件的变量 dir 显示当前目录或指定目录下的文件 diary 日志文件命令 clc 清除工作窗中的所有显示内容 ! 调用 DOS 命令 ...
- DataAnalysis-Pandas分组聚合
title: Pandas分组聚合 tags: 数据分析 python categories: DataAnalysis toc: true date: 2020-02-10 16:28:49 Des ...
- Powershell抓取网页信息
一般经常使用invoke-restmethod和invoke-webrequest这两个命令来获取网页信息,如果对象格式是json或者xml会更容易 1.invoke-restmethod 我们可以用 ...