1. Overview
1.概述
This article explores the Spring Boot TestRestTemplate. It can be treated as a follow-up of The Guide to RestTemplate, which we firmly recommend to read before focusing on TestRestTemplate. TestRestTemplate can be considered as an attractive alternative of RestTemplate.
本文探讨了Spring Boot的TestRestTemplate。它可以被视为RestTemplate指南的后续,我们强烈建议在关注TestRestTemplate之前阅读该指南。TestRestTemplate可以被认为是RestTemplate的一个有吸引力的替代品。
2. Maven Dependencies
2.Maven的依赖性
To use TestRestTemplate, you are required to have an appropriate dependency like:
要使用TestRestTemplate,你需要有一个适当的依赖关系,如。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
You can find the latest version on Maven Central.
您可以在Maven Central上找到最新版本。
3. TestRestTemplate and RestTemplate
3.TestRestTemplate和RestTemplate
Both of these clients are quite suitable for writing integration tests and can handle communicating with HTTP APIs very well.
这两个客户端都相当适用于编写集成测试,并且可以很好地处理与HTTP API的通信。
For example, they provide us with the same methods standard methods, headers, and other HTTP constructs.
例如,他们为我们提供了相同的方法标准方法、头文件和其他HTTP结构。
And all of these operations are well described in The Guide to RestTemplate, so we won’t revisit them here.
而所有这些操作在RestTemplate指南中都有很好的描述,所以我们在这里就不重述了。
Here’s a simple GET request example:
下面是一个简单的GET请求例子。
TestRestTemplate testRestTemplate = new TestRestTemplate();
ResponseEntity<String> response = testRestTemplate.
getForEntity(FOO_RESOURCE_URL + "/1", String.class);
Assertions.assertEquals(response.getStatusCode(), HttpStatus.OK);
Despite the fact that both classes are very similar, TestRestTemplate does not extend RestTemplate and does offer a few very exciting new features.
尽管这两个类都非常相似,但TestRestTemplate并没有扩展RestTemplate,而且确实提供了一些非常令人兴奋的新功能。
4. What’s New in TestRestTemplate?
4.TestRestTemplate中有什么新内容?
4.1. Constructor With Basic Auth Credentials
4.1.带有基本授权凭证的构造函数
TestRestTemplate provides a constructor with which we can create a template with specified credentials for basic authentication.
TestRestTemplate提供了一个构造函数,我们可以用它来创建一个具有指定凭证的模板,用于基本认证。
All requests performed using this instance will be authenticated using provided credentials:
使用该实例执行的所有请求都将使用所提供的凭证进行验证。
TestRestTemplate testRestTemplate
= new TestRestTemplate("user", "passwd");
ResponseEntity<String> response = testRestTemplate.
getForEntity(URL_SECURED_BY_AUTHENTICATION, String.class);
Assertions.assertEquals(response.getStatusCode(), HttpStatus.OK);
4.2. Constructor With HttpClientOption
4.2.使用HttpClientOption的构造函数
TestRestTemplate also enables us to customize the underlying Apache HTTP client using the HttpClientOption which is an enum in TestRestTemplate with the following options: ENABLE_COOKIES, ENABLE_REDIRECTS, and SSL.
TestRestTemplate还使我们能够使用HttpClientOption来定制底层的Apache HTTP客户端,它是TestRestTemplate中的一个枚举,有以下选项。ENABLE_COOKIES, ENABLE_REDIRECTS, 和SSL。
Let’s see a quick example:
让我们看一个快速的例子。
TestRestTemplate testRestTemplate = new TestRestTemplate("user",
"passwd", TestRestTemplate.HttpClientOption.ENABLE_COOKIES);
ResponseEntity<String> response = testRestTemplate.
getForEntity(URL_SECURED_BY_AUTHENTICATION, String.class);
Assertions.assertEquals(response.getStatusCode(), HttpStatus.OK);
In the above example, we’re using the options together with Basic Authentication.
在上面的例子中,我们将这些选项与基本认证一起使用。
If we don’t need authentication, we still can create a template with a simple constructor:
如果我们不需要认证,我们仍然可以用一个简单的构造函数创建一个模板。
TestRestTemplate(TestRestTemplate.HttpClientOption.ENABLE_COOKIES)
TestRestTemplate(TestRestTemplate.HttpClientOption.ENABLE_COOKIES)
4.3. New Method
4.3.新方法
Not only can constructors create a template with specified credentials. We can also add credentials after our template is created. TestRestTemplate gives us a method withBasicAuth() which adds credentials to an already existing template:
构造函数不仅可以用指定的凭证创建一个模板。我们还可以在模板创建后添加凭证。TestRestTemplate给了我们一个方法withBasicAuth() ,它可以将凭证添加到已经存在的模板中。
TestRestTemplate testRestTemplate = new TestRestTemplate();
ResponseEntity<String> response = testRestTemplate.withBasicAuth(
"user", "passwd").getForEntity(URL_SECURED_BY_AUTHENTICATION,
String.class);
Assertions.assertEquals(response.getStatusCode(), HttpStatus.OK);
5. Using Both TestRestTemplate and RestTemplate
5.同时使用TestRestTemplate和RestTemplate
TestRestTemplate can work as a wrapper for RestTemplate, e.g. if we are forced to use it because we are dealing with legacy code. You can see below how to create such a simple wrapper:
TestRestTemplate可以作为RestTemplate的包装器,例如,如果我们因为要处理遗留代码而被迫使用它。你可以在下面看到如何创建这样一个简单的包装器。
RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder();
restTemplateBuilder.configure(restTemplate);
TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplateBuilder);
ResponseEntity<String> response = testRestTemplate.getForEntity(
FOO_RESOURCE_URL + "/1", String.class);
Assertions.assertEquals(response.getStatusCode(), HttpStatus.OK);
6. Conclusion
6.结论
TestRestTemplate is not an extension of RestTemplate, but rather an alternative that simplifies integration testing and facilitates authentication during tests. It helps in customization of Apache HTTP client, but also it can be used as a wrapper of RestTemplate.
TestRestTemplate不是RestTemplate的扩展,而是一个替代方案,可以简化集成测试,方便测试期间的认证。它有助于定制Apache HTTP客户端,但也可以作为RestTemplate的封装器使用。
You can check out the examples provided in this article over on GitHub.
你可以查看本文在GitHub上提供的例子。