gpt4 book ai didi

java - 当rest模板具有使用Spring boot 1.5.x的拦截器时,我们可以使用@RestClientTest吗?

转载 作者:行者123 更新时间:2023-12-01 17:36:04 25 4
gpt4 key购买 nike

我正在使用 Spring Boot 1.5.x (Spring 4.2.x),并且创建了一个 RestClientSdk spring 组件类,如下所示:

@Component
public class RestClientSdkImpl implements RestClientSdk {

private RestTemplate restTemplate;

@Autowired
public RestClientSdkImpl(RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder.build();
}
...
//other methods kept out for brevity
}

我还定义了一个 DefaultRestTemplateCustomizer spring 组件,如下所示:

@Component
public class DefaultRestTemplateCustomizer implements RestTemplateCustomizer {

private LogClientHttpRequestInterceptor logClientHttpRequestInterceptor;

@Autowired
public DefaultRestTemplateCustomizer(LogClientHttpRequestInterceptor logClientHttpRequestInterceptor) {
this.logClientHttpRequestInterceptor = logClientHttpRequestInterceptor;
}

@Override
public void customize(RestTemplate restTemplate) {
restTemplate.getInterceptors().add(logClientHttpRequestInterceptor);
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
}

}

这样,我定义了一个测试类,如下所示,它使用 @RestClientTest 注释,如下所示。

@RunWith(SpringRunner.class)
@RestClientTest(RestClientSdk.class)
@ActiveProfiles("test")
/*
* The RestClientTest only includes the most minimal configuration to include a rest template builder,
* so we include the rest sdk auto config within the scope of the test
*/
@ImportAutoConfiguration(RestSdkAutoConfiguration.class)
public class RestClientApplicationBehaviourTest{

@Autowired
private RestClientSdk restClientSdk;

@Autowired
private MockRestServiceServer mockRestServiceServer;

/**
* A simple Http Get that retrieves a JSON document from a rest server and
* produces a plain old java object as a response.
*/
@Test
public void testPlainHttpGet() throws IOException{
//ARRANGE
RestClientDto<?> simpleGetRequest = simpleHttpGet();
mockRestServiceServer.expect(once(), requestTo("http://localhost:8080/account/1234567890"))
.andRespond(withSuccess(IOUtils.getInputAsString("/stubs/account.json"),MediaType.APPLICATION_JSON));
//ACT
Account account = restClientSdk.send(simpleGetRequest, Account.class);
//ASSERT
mockRestServiceServer.verify();
Assert.assertNotNull(account);
Assert.assertNotNull(account.getAccountId());
Assert.assertNotNull(account.getFirstName());
Assert.assertNotNull(account.getLastName());
}
...
//not including other methods for brevity
}
<小时/>

问题

<小时/>

由于 MockRestServiceServer 构建器使用 MockClientHttpRequestFactory 覆盖我的其余模板中的 BufferingClientHttpRequestFactory,因此我从正文中收到 null 响应。这是因为日志记录拦截器正在读取来自响应的输入流,因此该流不再有可读取的内容。 BufferingClientHttpRequestFactory 会阻止这种情况发生。现在,我知道从 Spring 5.0.5 开始,MockRestServiceServer 构建器中有一个名为 bufferContent 的额外选项,但我没有迁移到 Spring 5.x 的选项(Spring Boot 2 .x),所以我想知道是否有办法使用 Spring Boot 1.5.x/Spring 4.2.x 进行配置。

我先谢谢你了!

胡安

最佳答案

为了解决该问题,我需要定义一个测试配置,这将允许我覆盖客户端请求工厂。请参阅下面的代码。这有点 hacky,但我认为真正的解决方案是升级到 Spring 5.x/Spring Boot 2.x。

@Configuration
@Profile("test")
public class MockRestServiceServerConfiguration {

/**
* Wrap the Mock Rest client factory in the buffered one.
* @param restClientSdk The rest client SDK containing the rest template to use.
* @return The mock rest service server to use.
*/
@Bean
public MockRestServiceServer mockRestServiceServer(RestClientSdkImpl restClientSdkImpl) {
RestTemplate restTemplate = restClientSdkImpl.getRestTemplate();
MockRestServiceServer server = MockRestServiceServer.createServer(restTemplate);

//need to do this because getRequestFactory returns InterceptingHttpRequestFactory wraping the mock rest service server request factory
List<ClientHttpRequestInterceptor> templateInterceptors = restTemplate.getInterceptors();
restTemplate.setInterceptors(null);

//now we wrap the delegate, which should be the mock rest service server request factory
BufferingClientHttpRequestFactory bufferingFact = new BufferingClientHttpRequestFactory(restTemplate.getRequestFactory());
restTemplate.setRequestFactory(bufferingFact);
restTemplate.setInterceptors(templateInterceptors);
return server;
}
}

关于java - 当rest模板具有使用Spring boot 1.5.x的拦截器时,我们可以使用@RestClientTest吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61038912/

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