gpt4 book ai didi

testing - 如何测试 Apache HttpClient RequestConfig 值是否设置正确?没有公共(public) setter/getter 存在

转载 作者:行者123 更新时间:2023-11-28 21:37:58 26 4
gpt4 key购买 nike

我有这个类来配置一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com