From 10732bd39a81171ebbe9169e7a602a5a25299fb8 Mon Sep 17 00:00:00 2001 From: qibaoguang Date: Fri, 27 Feb 2015 21:27:36 +0800 Subject: [PATCH] Update 35.4.4. TestRestTemplate.md --- .../35.4.4. TestRestTemplate.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/IV. Spring Boot features/35.4.4. TestRestTemplate.md b/IV. Spring Boot features/35.4.4. TestRestTemplate.md index e69de29..6849f38 100644 --- a/IV. Spring Boot features/35.4.4. TestRestTemplate.md +++ b/IV. Spring Boot features/35.4.4. TestRestTemplate.md @@ -0,0 +1,13 @@ +### 35.4.4. TestRestTemplate + +TestRestTemplate是一个方便进行集成测试的Spring RestTemplate子类。你会获取到一个普通的模板或一个发送基本HTTP认证(使用用户名和密码)的模板。在任何情况下,这些模板都表现出对测试友好:不允许重定向(这样你可以对响应地址进行断言),忽略cookies(这样模板就是无状态的),对于服务端错误不会抛出异常。推荐使用Apache HTTP Client(4.3.2或更好的版本),但不强制这样做。如果在classpath下存在Apache HTTP Client,TestRestTemplate将以正确配置的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")); +} +} +```