2017-10-17 62 views
0

如何將TestRestTemplate.HttpClientOption.ENABLE_REDIRECTSTestRestTemplate.HttpClientOption.ENABLE_COOKIES添加到spring-boots TestRestTemplate?在TestRestTemplate中啓用Cookie和重定向

我正在使用spring-boot,因此我自動爲我配置了TestRestTemplate。

我可以在創建之前使用RestTemplateBuilder來定製此bean。問題是我不能看到如何添加這些選項:

@Bean 
    public RestTemplateBuilder restTemplateBuilder() { 
     return new RestTemplateBuilder() 
       .errorHandler(new ResponseErrorHandler() { 
        ... 
       }); 
    } 

documentation有一些構造函數接受這些選項,但問題是,已經爲我創建的豆。

回答

1

您可以創建一個TestRestTemplate,並使用@Bean註釋將其呈現給Spring。

例如:

@Bean 
@Primary 
public TestRestTemplate testRestTemplate() { 
    RestTemplate restTemplate = new RestTemplateBuilder() 
      .errorHandler(new ResponseErrorHandler() { 
       @Override 
       public boolean hasError(ClientHttpResponse response) throws IOException { 
        return false; 
       } 

       @Override 
       public void handleError(ClientHttpResponse response) throws IOException { 

       } 
      }).build(); 

    return new TestRestTemplate(restTemplate, user, password, TestRestTemplate.HttpClientOption.ENABLE_REDIRECTS, TestRestTemplate.HttpClientOption.ENABLE_COOKIES); 
} 

或者,如果您不需要定製RestTemplate然後使用下面的構造函數(它在內部實例爲您RestTemplate):

@Bean 
@Primary 
public TestRestTemplate testRestTemplate() { 
    return new TestRestTemplate(TestRestTemplate.HttpClientOption.ENABLE_REDIRECTS, TestRestTemplate.HttpClientOption.ENABLE_COOKIES); 
} 

更新1來解決此評論:

當我運行我的測試,現在我得到以下錯誤org.apache.http.ProtocolException: Target host is not specified

Spring提供的TestRestTemplate被配置爲解決相對於http://localhost:${local.server.port}路徑。因此,當您用自己的實例替換Spring提供的實例時,您需要提供完整的地址(包括主機和端口)或者配置LocalHostUriTemplateHandler(您可以在org.springframework.boot.test.context.SpringBootTestContextCustomizer.TestRestTemplateFactory中看到此代碼)來配置您自己的TestRestTemplate。下面是後一種方法的一個例子:

@Bean 
@Primary 
public TestRestTemplate testRestTemplate(ApplicationContext applicationContext) { 
    RestTemplate restTemplate = new RestTemplateBuilder() 
      .errorHandler(new ResponseErrorHandler() { 
       @Override 
       public boolean hasError(ClientHttpResponse response) throws IOException { 
        return false; 
       } 

       @Override 
       public void handleError(ClientHttpResponse response) throws IOException { 

       } 
      }).build(); 

    TestRestTemplate testRestTemplate = 
      new TestRestTemplate(restTemplate, user, password, TestRestTemplate.HttpClientOption 
        .ENABLE_REDIRECTS, TestRestTemplate.HttpClientOption.ENABLE_COOKIES); 

    // let this testRestTemplate resolve paths relative to http://localhost:${local.server.port} 
    LocalHostUriTemplateHandler handler = 
      new LocalHostUriTemplateHandler(applicationContext.getEnvironment(), "http"); 
    testRestTemplate.setUriTemplateHandler(handler); 

    return testRestTemplate; 
} 

有了這個bean配置以下測試案例使用了定製的TestRestTemplate併成功調用本地主機上的春天啓動的應用程序:

@RunWith(SpringRunner.class) 
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 
public class RestTemplateTest { 

    @Autowired 
    private TestRestTemplate restTemplate; 

    @Test 
    public void test() { 
     ResponseEntity<String> forEntity = this.restTemplate.getForEntity("/some/endpoint", String.class); 
     System.out.println(forEntity.getBody()); 
    } 
} 
+0

這將導致更多的問題。首先,我需要在'testRestTemplate()'中添加'@ Primary',另外註冊兩個bean。其次,當我運行測試時,我現在得到以下錯誤:org.apache.http.ProtocolException:未指定目標主機。我認爲在創建TestRestTemplate時,spring-boot會有更多的魔力。 – jax

+0

@jax道歉,我已經更新了這個問題,以澄清如何使用定製的'TestRestTemplate'以及如何使用它,就像你使用Spring提供的'TestRestTemplate'一樣。 – glytching