spring_reference/IV. Spring Boot features/35.4.4. TestRestTemplate.md

14 lines
954 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

### 35.4.4. TestRestTemplate
TestRestTemplate是一个方便进行集成测试的Spring RestTemplate子类。你会获取到一个普通的模板或一个发送基本HTTP认证使用用户名和密码的模板。在任何情况下这些模板都表现出对测试友好不允许重定向这样你可以对响应地址进行断言忽略cookies这样模板就是无状态的对于服务端错误不会抛出异常。推荐使用Apache HTTP Client(4.3.2或更好的版本)但不强制这样做。如果在classpath下存在Apache HTTP ClientTestRestTemplate将以正确配置的client进行响应。
```java
public class MyTest {
RestTemplate template = new TestRestTemplate();
@Test
public void testRequest() throws Exception {
HttpHeaders headers = template.getForEntity("http://myhost.com", String.class).getHeaders();
assertThat(headers.getLocation().toString(), containsString("myotherhost"));
}
}
```