You can try to add a HttpRequestInterceptor before building the actual client, after getting the client from your utils class (which you want to test)....import org.apache.http.HttpRequestInterceptor;import org.apache.http.client.config.RequestConfig;import org.apache.http.client.protocol.HttpClientContext;import org.apache.http.protocol.HttpContext;import org.apache.http.HttpRequest;import org.apache.http.HttpException;...builder.addInterceptorFirst(new HttpRequestInterceptor() { @Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { // Get hold of the client context, which holds the request config RequestConfig requestConfig = HttpClientContext.adapt(context).getRequestConfig(); assertEquals(10, requestConfig.getConnectTimeout()); assertEquals(20, requestConfig.getConnectionRequestTimeout()); assertEquals(30, requestConfig.getSocketTimeout()); } });现在只需对任何URL进行调用,并忽略客户端对URL的请求失败时将抛出的异常.由于拦截器是首先添加的,并且是请求拦截器,因此应在尝试请求之前调用该拦截器,从而为请求配置提供断言.Now just do a call to any URL and ignore the exception the client is going to throw in case the request to the URL fails. Since the interceptor is added as first and is a request interceptor, it should be called before the request is attempted, giving you the request config to assert. 这篇关于Apache HttpClient中的单元测试超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-17 11:51
查看更多