Spring 引导执行器中断 @AutoConfigureMockRestServiceServer

Spring boot actuator breaks @AutoConfigureMockRestServiceServer

我有一个 class 可以构建多个 RestTemplates using RestTemplateBuilder:

private RestTemplate build(RestTemplateBuilder restTemplateBuilder) {
    return restTemplateBuilder
      .rootUri("http://localhost:8080/rest")
      .build();
}

对于我的测试设置,我使用 @AutoConfigureMockRestServiceServer and mock responses using MockServerRestTemplateCustomizer:

mockServerRestTemplateCustomizer.getServer()
  .expect(ExpectedCount.times(2),
        requestToUriTemplate("/some/path/{withParameters}", "withParameters"))
    .andRespond(withSuccess());

当我取消注释我的 pom 中的 spring-boot-actuator 依赖项时,我的测试通过了,而在另一种情况下失败并显示以下消息。

Expected: /some/path/parameter
Actual: http://localhost:8080/rest/pos/some/path/withParameters

我通过 MockServerRestTemplateCustomizer that spring-boot-actuator applies a "DelegateHttpClientInterceptor" for supporting their built in metrics for rest templates. However this creates a problem with the following code which I found in RootUriRequestExpectationManager 调试发现:

public static RequestExpectationManager forRestTemplate(RestTemplate restTemplate,
        RequestExpectationManager expectationManager) {
    Assert.notNull(restTemplate, "RestTemplate must not be null");
    UriTemplateHandler templateHandler = restTemplate.getUriTemplateHandler();
    if (templateHandler instanceof RootUriTemplateHandler) {
        return new RootUriRequestExpectationManager(((RootUriTemplateHandler) templateHandler).getRootUri(),
                expectationManager);
    }
    return expectationManager;
}

因为如上所述 spring-boot-actuator 注册了一个 "DelegateHttpClientInterceptor" which leads to the above code not recognizing the RootUriTemplateHandler and therefore not matching the request using requestToUriTemplate.

我在这里缺少什么才能使它正常工作?

Andy Wilkinson pointed out, this seems to be a bug in Spring boot. I created an issue with a sample project.