无法使用两种方法使用HttpClient的同一对象运行测试用例

时间:2018-11-30 10:29:12

标签: java spring-boot jenkins

这是詹金斯的例外:

错误消息

Invalid use of BasicClientConnManager: connection still allocated.
Make sure to release the connection before allocating another one.

Stacktrace

java.lang.IllegalStateException: 
Invalid use of BasicClientConnManager: connection still allocated.
Make sure to release the connection before allocating another one.
at com.idsk.ftrequestor.NotificationFetchTaskTest.testDownloadContent(NotificationFetchTaskTest.java:93)

它在本地环境下工作,但在詹金斯上却没有。

@Test
public void getAllNotificationsTest() throws URISyntaxException, java.text.ParseException, IOException {
     URI notoficationURL=null;
    String validDateTime = null;
    validDateTime = Utils.getValidDateTime(startDateTime);
    notoficationURL=constructRequest(ftpNotificationServiceTest.getNotificationUri(),validDateTime);
    HttpResponse response = null;
    NotificationResponse notificationResponse = null;
    try {
        response = restUtilsTest.call(notoficationURL);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode == HttpStatus.SC_OK) {
            final HttpEntity entity = response.getEntity();
            notificationResponse = populateObject(entity);
        }
    } finally {
        if (response != null) {
                response.getEntity().getContent().close();
        }
    }
    assertNotNull("All Notifications not retrieved  .", notificationResponse);
}

1 个答案:

答案 0 :(得分:0)

您可以简单地尝试根据所使用的请求类型GET / POST释放连接。 请参考对代码所做的以下更改,然后根据您的配置对其进行修改。

@Test
public void getAllNotificationsTest() throws URISyntaxException, java.text.ParseException, IOException {
    URI notoficationURL = null;
    HttpGet httpGetRequest = null;
    String validDateTime = null;
    validDateTime = Utils.getValidDateTime(startDateTime);
    notoficationURL = constructRequest(ftpNotificationServiceTest.getNotificationUri(), validDateTime);
    HttpResponse response = null;
    NotificationResponse notificationResponse = null;
    try {
        httpGetRequest = new HttpGet(notoficationURL);
        httpGetRequest.addHeader(Constants.X_API_KEY, xApiKey);
        httpGetRequest.addHeader(Constants.CONTENT_TYPE, contentType);
        response = ftpHttpClientTest.execute(httpGetRequest);

        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode == HttpStatus.SC_OK) {
            final HttpEntity entity = response.getEntity();
            notificationResponse = populateObject(entity);
        }
    } finally {
        if (response != null) {
            response.getEntity().getContent().close();
        }
        if (httpGetRequest != null) {
            try {
                httpGetRequest.releaseConnection();
            } catch (Exception e) {
            }
        }
    }
    assertNotNull("All Notifications not retrieved  .", notificationResponse);
}