2016-04-21 35 views
1

我正在使用RestAssured 2.4.1測試第一個服務通過REST公開的服務堆棧。使用Restassured測試後端超時

現在,我想測試後端沒有響應時的行爲,這是REST服務應該檢測和處理的情況。不幸的是,RestAssured會在REST服務檢測到後端超時之前終止POST請求。

如何增加RestAssured的相應超時時間?我在嘗試以下方法但沒有成功

RestAssuredConfig config = RestAssured.config(); 
config.getHttpClientConfig() 
      .setParam(ClientPNames.CONN_MANAGER_TIMEOUT, 0) // HttpConnectionManager connection return time 
      .setParam(CoreConnectionPNames.CONNECTION_TIMEOUT, 0) // Remote host connection time 
      .setParam(CoreConnectionPNames.SO_TIMEOUT, 0) ; // Remote host response time 

given() 
    .config(config) 
    . ... 

回答

-1

什麼時候超時?

如果您的測試使用的是org.junit.Test,那麼您可以明確設置超時。下面將超時時間設置爲60秒(60000毫秒):

@Test(timeout = 60000) 
public void canCallFullyQualifiedUrlsWithoutPortDefined() throws Exception { 
    // This test hangs forever unless it works 
    get("http://filehost-semc-rss-dev.s3.amazonaws.com/testfile1.txt"); 
} 

你可以從靜止保證的實際響應時間:

@Test public void 
response_time_can_be_extracted() { 
    long time = 
    given(). 
      param("firstName", "John"). 
      param("lastName", "Doe"). 
    when(). 
      get("/greet"). 
    then(). 
      extract().response().time(); 

    assertThat(time, greaterThan(0L)); 
} 
+0

這完全沒有幫助。這個問題是關於增加超時,而不是甚至提前終止。 – morty

+0

@morty在這種情況下,你是否應該將超時設置爲高於60000?有趣。 – MikeJRamsey56

+0

這個問題清楚地表明RestAssured超時。在Junit終止測試之前設置或增加時間並不能解決問題,因爲它對RestAssured沒有任何影響。如果Junit的默認超時時間低於RestAssured的超時時間並且可以解決問題,那麼您的答案是正確的。 – morty

2

我使用RestAssured 3.0.1,但這是我如何解決這個問題 - 但全球化(我真的想降低默認的超時時間)。我也使用SystemDefaultHttpClient,因爲我想重用連接(HTTP keepalive)。因此它不適合100%,但可能會指出。

HttpClientConfig clientConfig = RestAssured.config().getHttpClientConfig(); 
clientConfig = clientConfig.httpClientFactory(new HttpClientConfig.HttpClientFactory() { 
    @Override 
    public HttpClient createHttpClient() { 
     HttpClient rv = new SystemDefaultHttpClient(); 
     HttpParams httpParams = rv.getParams(); 
     HttpConnectionParams.setConnectionTimeout(httpParams, 5 * 1000); //Wait 5s for a connection 
     HttpConnectionParams.setSoTimeout(httpParams, 60 * 1000); // Default session is 60s 
     return rv; 
    } 
}); 

//This is necessary to ensure, that the client is reused. 
clientConfig = clientConfig.reuseHttpClientInstance(); 

RestAssured.config = RestAssured.config().httpClient(clientConfig);