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使用的更多相关文章

  1. spring boot使用TestRestTemplate集成测试 RESTful 接口

    这篇文章没什么技术含量,只是单纯的记录一下如何用TestRestTemplate访问受security保护的api,供以后查阅. @Slf4j @RunWith(SpringRunner.class) ...

  2. 玩转spring boot——快速开始

    开发环境: IED环境:Eclipse JDK版本:1.8 maven版本:3.3.9 一.创建一个spring boot的mcv web应用程序 打开Eclipse,新建Maven项目 选择quic ...

  3. spring boot单元测试(转)

    Junit这种老技术,现在又拿出来说,不为别的,某种程度上来说,更是为了要说明它在项目中的重要性.凭本人的感觉和经验来说,在项目中完全按标准都写Junit用例覆盖大部分业务代码的,应该不会超过一半. ...

  4. 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 ...

  5. (转)Spring Boot Junit单元测试

    场景:在项目开发中要测试springboot工程中一个几个dao和service的功能是否正常,初期是在web工程中进行要素的录入,工作量太大.使用该单元测试大大减小了工作强度. Junit这种老技术 ...

  6. Spring Boot单元测试(Mock)

    Spring Boot单元测试(Mock) Java个人学习心得 2017-08-12 16:07 Mock 单元测试的重要性就不多说了,我这边的工程一般都是Spring Boot+Mybatis(详 ...

  7. Spring Boot 1.4测试的改进

    对Pivotal团队来说,工作上的好事情是他们拥有一个被叫做Pivotal Labs的灵活发展部门,拥有Labs团队的Lean 和 XP程序设计方法学的强大支持,例如结对编程和测试驱动开发.他们对于测 ...

  8. Spring Boot 1.4测试的简单理解

    首先maven要引入spring-boot-starter-test这个包. 先看一段代码 @RunWith(SpringRunner.class) @SpringBootTest(webEnviro ...

  9. 详细介绍Spring Boot 2.0的那些新特性与增强

    以Java 8 为基准 Spring Boot 2.0 要求Java 版本必须8以上, Java 6 和 7 不再支持. 内嵌容器包结构调整 为了支持reactive使用场景,内嵌的容器包结构被重构了 ...

随机推荐

  1. Step by Step!教你如何在k3s集群上使用Traefik 2.x

    本文来自边缘计算k3s社区 作者简介 Cello Spring,瑞士人.从电子起步,拥有电子工程学位.尔后开始关注计算机领域,在软件开发领域拥有多年的工作经验. Traefik是一个十分可靠的云原生动 ...

  2. 使用错误代码对象进行C++错误处理

    原文发表于codeproject,由本人翻译整理分享于此. 前言 我已经使用了本文描述的代码和机制近20年了,到目前为止,我还没有找到更好的方法来处理大型C++项目中的错误.最初的想法是从一篇文章(D ...

  3. SpringBoot系列之RabbitMQ使用实用教程

    SpringBoot系列之RabbitMQ使用实用教程 @ 目录 1. 消息队列概述 1.1 MQ的概述 1.2 MQ目的地形式 2. 消息队列实现方式 2.1 常见MQ框架 2.2 MQ实现方式 3 ...

  4. MySQL从库实用技能(一)--巧用slave_exec_mode参数

    想必从库异常中断的情况不在少数,其中报错信息中1032及1062的错误占了不少的比重 错误1032指的是从库中找不到对应行的记录 错误1062指的是主键冲突 遇到此报错时,大多DBA会使用如下方法进行 ...

  5. C++动态内存new和delete(超详细)

    C++动态内存new和delete C++动态内存是C++灵活.炫酷的一种操作.学好它,能让自己编程逼格上一个level. 在学习动态内存之前,我们先要了解C++是怎么划分内存的: 栈:在函数内部声明 ...

  6. LeetCode 题解 | 70. 爬楼梯

    假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解释: 有两 ...

  7. shell查询目标jvm的perm占比

    #查询指定进程号下面的方法区使用率,jdk1.7是perm,jdk1.8是metaspace function get_perm_use_percent() { pid="$1" ...

  8. Centos装机预备技能

                                                               装机预备技能 1.1问题 本例要求安装一台可用的KVM服务器: RHEL与Cent ...

  9. Thinkphp getLastSql函数用法

    如何判断一个更新操作是否成功: $Model = D('Blog'); $data['id'] = 10; $data['name'] = 'update name'; $result = $Mode ...

  10. 关于机械硬盘的 CMR 和 SMR 技术

    文章更新于:2020-02-21 部分内容是非原创的截图 一.机械硬盘价格区间 这些都是在天猫上面找的一些普遍价格,型号不完全对应,只是简单做个比较. 其中西数天猫旗舰店2TB硬盘64MB硬盘已经下架 ...