- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这个类来配置一个 HttpClient
实例:
package com.company.fraud.preauth.service.feignaccertifyclient;
import com.company.fraud.preauth.config.ProviderClientConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.ssl.SSLContextBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
@Slf4j
@Configuration
@RequiredArgsConstructor
public class FeignClientConfig {
private final ProviderClientConfig providerClientConfig;
public HttpClient buildHttpClient() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
RequestConfig.Builder requestBuilder = RequestConfig.custom();
requestBuilder.setConnectTimeout(providerClientConfig.getConnectionTimeout());
requestBuilder.setConnectionRequestTimeout(providerClientConfig.getConnectionRequestTimeout());
requestBuilder.setSocketTimeout(providerClientConfig.getSocketTimeout());
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
return HttpClientBuilder.create()
.setMaxConnPerRoute(providerClientConfig.getMaxConnectionNumber())
.setDefaultRequestConfig(requestBuilder.build())
.setSSLContext(builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()).build())
.build();
}
}
如何对此类进行单元测试,以查看结果 HttpClient
是否正确设置了这些值?
从 httpClient
我无法访问它的 RequestConfig
。
我知道这两个帖子:
How do I test a private function or a class that has private methods, fields or inner classes?(这个问题的投票数表明它是测试中一个并发且有争议的话题,我的情况可以提供一个例子,说明为什么我们应该在测试中查看实例的内部状态,尽管它是私有(private)的)
Unit test timeouts in Apache HttpClient(它显示了一种在代码中添加拦截器以检查配置值的方法,但我不喜欢它,因为我想将测试与功能代码分开)
有什么办法吗?我明白这个类应该被测试,对吧?您不能盲目地相信它会起作用;并检查它“notNull”对我来说似乎很脆弱。
此链接可能会为我指明正确的方向:
https://dzone.com/articles/testing-objects-internal-state
它使用 PowerMock.Whitebox
检查实例的内部状态。
最佳答案
所以我查看了 PowerMock.Whitebox
源代码,结果发现内部使用了反射。而且,由于据说 PowerMock 与 JUnit 5 不兼容(直到现在),而且我不想添加另一个仅用于测试的依赖项,所以我将使用反射进行测试。
package com.company.fraud.preauth.service.feignaccertifyclient;
import com.company.fraud.preauth.config.PreAuthConfiguration;
import com.company.fraud.preauth.config.ProviderClientConfig;
import com.company.fraud.preauth.config.StopConfiguration;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.lang.reflect.Field;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {
PreAuthConfiguration.class,
StopConfiguration.class,
})
public class FeignClientConfigTest {
@Mock
private ProviderClientConfig providerClientConfig;
@Test
@DisplayName("should return HttpClient with defaultConfig field filled with values in providerClientConfig")
public void shouldReturnHttpClientWithConfiguredValues() throws Exception {
// given
when(providerClientConfig.getConnectionRequestTimeout()).thenReturn(30000);
when(providerClientConfig.getConnectionTimeout()).thenReturn(30);
when(providerClientConfig.getMaxConnNumPerRoute()).thenReturn(20);
when(providerClientConfig.getSocketTimeout()).thenReturn(10);
FeignClientConfig feignClientConfig = new FeignClientConfig(providerClientConfig);
// when
HttpClient httpClient = feignClientConfig.buildHttpClient();
// then
// I want to test internal state of built HttpClient and this should be checked
// I tried to use PowerMock.Whitebox, but then I found it uses reflection internally
// I don't want to introduce another dependency, and PowerMock is said not to be compatible with JUnit 5, so..
Field requestConfigField = httpClient.getClass().getDeclaredField("defaultConfig");
requestConfigField.setAccessible(true);
RequestConfig requestConfig = (RequestConfig)requestConfigField.get(httpClient);
assertThat(requestConfig.getConnectionRequestTimeout(), equalTo(30000));
assertThat(requestConfig.getConnectTimeout(), equalTo(30));
assertThat(requestConfig.getSocketTimeout(), equalTo(10));
}
}
此外,我回答了 OP 中关于何时测试类中私有(private)成员的第一个问题 here
关于testing - 如何测试 Apache HttpClient RequestConfig 值是否设置正确?没有公共(public) setter/getter 存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56098377/
我正在使用 Apache HTTP 异步客户端,我必须对其进行一些配置。 我有以下代码,但在设置 RequestConfig 和 IOReactorConfig 时我很困惑,因为您可以为它们指定超时配
我继承了代码 import org.apache.http.client.HttpClient; ... HttpClient httpclient = createHttpClientOrProxy
我有以下 Java 代码: RequestConfig.custom().setSocketTimeout(10).setConnectTimeout(20).build(); 如何在 Python
我继承了代码 HttpPost httpPost = new HttpPost(url); httpPost.getParams().setBooleanParameter(
这个问题在这里已经有了答案: android: no class def found error from library project (4 个答案) 关闭 8 年前。 我将 Parse4j 导
我在 SocketConfig 中设置了套接字超时,并将 SocketConfig 设置为连接管理器 connManager.setDefaultSocketConfig(SocketConfig.c
在 Apache HttpClient 4.3 中构建 CloseableHttpClient 时,我可以使用 SocketConfig.custom().setSoTimeout(soTimeout
我有这个类来配置一个 HttpClient 实例: package com.company.fraud.preauth.service.feignaccertifyclient; import
以下是我的代码 RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeou
我是一名优秀的程序员,十分优秀!