Spring Boot的TestRestTemplate使用
文章目录
- 添加maven依赖
- TestRestTemplate VS RestTemplate
- 使用Basic Auth Credentials
- 使用HttpClientOption
- 使用RestTemplateBuilder
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基本的使用:
@Testpublic 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
更多精彩内容且看:
- 区块链从入门到放弃系列教程-涵盖密码学,超级账本,以太坊,Libra,比特币等持续更新
- Spring Boot 2.X系列教程:七天从无到有掌握Spring Boot-持续更新
- Spring 5.X系列教程:满足你对Spring5的一切想象-持续更新
- java程序员从小工到专家成神之路(2020版)-持续更新中,附详细文章教程
更多教程请参考 flydean的博客
Spring Boot的TestRestTemplate使用相关推荐
- Spring Boot 2.X系列教程:七天从无到有掌握Spring Boot-持续更新
文章目录 简介 Spring Boot的基本操作 Spring Boot的构建和部署 Spring Boot工具 Spring Boot的测试 Spring Boot中使用JPA Spring Boo ...
- Spring Boot特性
2019独角兽企业重金招聘Python工程师标准>>> SpringApplication SpringApplication类提供了一种从main()方法启动Spring应用的便捷 ...
- Spring Boot 2.4.3、2.3.9 版本发布,你准备好了吗?
新年上班第一天,Spring Boot 就发布了最新的2.4.3 .2.3.9 版本.心急的小伙伴现在可从 repo.spring.io 和 Maven Central 获得相关内容. <par ...
- Spring Boot 的单元测试和集成测试
点击蓝色"程序猿DD"关注我 回复"资源"获取独家整理的学习资料! 作者 | 万想 来源 | 公众号「锅外的大佬」 学习如何使用本教程中提供的工具,并在 Spr ...
- Spring Boot 2.0 新特性
作者:贺卓凡 原文:https://mp.weixin.qq.com/s/EWmuzsgHueHcSB0WH-3AQw 以Java 8 为基准 Spring Boot 2.0 要求Java 版本必须8 ...
- Spring Boot 2.0 新特性和发展方向
以Java 8 为基准 Spring Boot 2.0 要求Java 版本必须8以上, Java 6 和 7 不再支持. 内嵌容器包结构调整 为了支持reactive使用场景,内嵌的容器包结构被重构了 ...
- spring boot单元测试
做一个稳健的开发,写一首漂亮的单元测试是少不了的 首先要分清几个概念:测试方法.测试类.测试集.测试运行器. 测试方法就是用 @Test 注解的一些函数. 测试类是包含一个或多个测试方法的一个 XxT ...
- Spring Boot (1) 构建第一个Spring Boot工程
Spring boot简介 spring boot是spring官方推出的一个全新框架,其设计目的是用来简化新spring应用的初始搭建以及开发过程. Spring boot特点 1.化繁为简,简化配 ...
- Spring Boot(十二)单元测试JUnit
一.介绍 JUnit是一款优秀的开源Java单元测试框架,也是目前使用率最高最流行的测试框架,开发工具Eclipse和IDEA对JUnit都有很好的支持,JUnit主要用于白盒测试和回归测试. 白盒测 ...
最新文章
- SSH学习-Hibernate关联查询
- 记一次vue2路由参数传递this指针问题
- 从针对接口编程到依赖注入
- axure9 邮件点击效果_EDM邮件营销,你不得不知道的8个指标?
- python装饰器setter_第7.27节 Python案例详解: @property装饰器定义属性访问方法getter、setter、deleter...
- Blazor VS React / Angular / Vue.js
- 使用Apache JMeter负载测试Web应用程序
- html加载js文件失败,firefox/chrome动态设置script加载js文件失败
- 在Eclispe中编译运行jbox2d
- pip 安装 scipy 出现错 no lapack/blas resources
- systemctl的使用
- c语言二级选择题APP,C语言二级题库
- 使用前端方式挑战 Chrome 小恐龙游戏高分
- Java中如何保证线程安全
- php个人微信公众号,PHP实现微信公众号自定义菜单
- 微信小程序开发入门与实战(Behaviors使用)
- 程序员慵懒的周末:不想出门还想吃肯德基香辣鸡翅?
- HEVC函数入门(22)——变换量化
- clickonce程序部署后,启动不成功的问题
- 51单片机模拟数字键盘输入以及图像显示+Proteus仿真