Я хочу макет RestTemplate, который является ответом функции:
@Configuration
@Data
public class ElasticConfiguration {
@Bean
public RestTemplate customRestTemplate(){
ClientHttpRequestFactory factory = new BufferingClientHttpRequestFactory(new
SimpleClientHttpRequestFactory());
RestTemplate restTemplate = new RestTemplate(factory);
restTemplate.setInterceptors(Collections.singletonList(new RequestResponseLoggingInterceptor()));
return restTemplate;
}
Это RequestResponseLoggingInterceptor, просто журнал отдыха для запроса и ответа журнала, и я думаю, что это проблема.
public class RequestResponseLoggingInterceptor implements ClientHttpRequestInterceptor {
private final Logger log = LoggerFactory.getLogger(this.getClass());
@Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
logRequest(request, body);
ClientHttpResponse response = execution.execute(request, body);
logResponse(response);
return response;
}
private void logResponse(ClientHttpResponse response) throws IOException {
if (log.isDebugEnabled()) {
log.debug("============================response begin==========================================");
log.debug("Status code : {}", response.getStatusCode());
log.debug("Status text : {}", response.getStatusText());
log.debug("Headers : {}", response.getHeaders());
log.debug("Response body: {}", StreamUtils.copyToString(response.getBody(), Charset.defaultCharset()));
log.debug("=======================response end=================================================");
}
}
private void logRequest(HttpRequest request, byte[] body) throws UnsupportedEncodingException {
if (log.isDebugEnabled()) {
log.debug("===========================request begin================================================");
log.debug("URI : {}", request.getURI());
log.debug("Method : {}", request.getMethod());
log.debug("Headers : {}", request.getHeaders());
log.debug("Request body: {}", new String(body, "UTF-8"));
log.debug("==========================request end================================================");
}
}
}
и в моем тестовом классе я делаю:
@Mock
RestTemplate restTemplateMock;
@Test
public void measureChannelProcessor() throws IOException {
when(restTemplateMock.postForEntity(anyString(), any(HttpEntity.class), eq(String.class)))
.thenReturn(
new ResponseEntity<>(resp,HttpStatus.OK));
when(elasticServiceMock.insert(anyString(),anyString())).thenAnswer(invocation -> {
String index = (String) invocation.getArguments()[0];
String message = (String) invocation.getArguments()[1];
String requestUri = new StringBuilder()
.append(elasticConfiguration.baseRequestBuilder(index))
.toString();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> requestEntity = new HttpEntity<>(message, headers);
**restTemplateMock = elasticConfiguration.customRestTemplate();**
return restTemplateMock.postForEntity(requestUri, requestEntity, String.class);
}
Но restTemplateMock равен нулю, если я опущу эту строку restTemplateMock = elasticConfiguration.customRestTemplate();
, все в порядке .. Несколько советов? с уважением