Spring MockRestServiceServer未及时更新运行状况终结点的restTemplate

Spring MockRestServiceServer未及时更新运行状况终结点的restTemplate,spring,groovy,spring-boot,spring-test,spring-boot-actuator,Spring,Groovy,Spring Boot,Spring Test,Spring Boot Actuator,我最近将SpringBoot项目从1.1升级到1.4,突然,“/health”端点的测试开始失败 @SpringBootTest class HealthTest extends Specification { @Autowired RestTemplate thirdPartyRestTemplate def 'should set health status based on third party service'() { given: MockRestSe

我最近将SpringBoot项目从1.1升级到1.4,突然,“/health”端点的测试开始失败

@SpringBootTest
class HealthTest extends Specification {

  @Autowired
  RestTemplate thirdPartyRestTemplate

  def 'should set health status based on third party service'() {
    given:
    MockRestServiceServer thirdPartyServerMock = MockRestServiceServer.createServer(thirdPartyRestTemplate)

    thirdPartyServerMock
      .expect(MockRestRequestMatchers.requestTo('/status'))
      .andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
      .andRespond(MockRestResponseCreators.withStatus(thirdPartyServerResponse).body('{}'))

    when:
    def response = RestAssured.given().get('/health')
    println(response.statusCode)
    Map health = response.as(Map)

    then:
    thirdPartyServerMock.verify()
    health == expectedHealth

    where:
    thirdPartyServerResponse       | expectedHealth
    HttpStatus.OK                  | [status: 'UP', thirdPartyServer: 'UP']
    HttpStatus.SERVICE_UNAVAILABLE | [status: 'DOWN', thirdPartyServer: 'DOWN']
  }

}
发生的事情是:第一个测试总是通过,而第二个测试总是失败。输出为200。如果顺序颠倒,也会发生同样的情况,所以

where:
    thirdPartyServerResponse       | expectedHealth
    HttpStatus.SERVICE_UNAVAILABLE | [status: 'DOWN', thirdPartyServer: 'DOWN']
    HttpStatus.OK                  | [status: 'UP', thirdPartyServer: 'UP']
这一次,503失败了。如果我在实际REST调用之前添加Thread.sleep行,如下所示

when:
    Thread.sleep(1000)
    def response = RestAssured.given().get('/health')
那它每次都会过去!因此,Spring似乎在
MockRestServiceServer
中引入了一些更改,并且需要一些时间来配置一个模拟(可能现在在单独的线程中执行)


有人有类似的问题吗?如何绕过此Thread.sleep修复程序以及对此行为的解释是什么?

结果表明,Spring为健康端点引入了缓存机制,默认情况下,它将结果缓存1秒。从文档中:

# Time to live for cached result, in milliseconds.    
endpoints.health.time-to-live=1000 
一旦我将配置更改为:

endpoints:
  health:
    time-to-live: 0

测试再次开始通过。

结果表明,Spring为健康端点引入了缓存机制,默认情况下,它将结果缓存1秒。从文档中:

# Time to live for cached result, in milliseconds.    
endpoints.health.time-to-live=1000 
一旦我将配置更改为:

endpoints:
  health:
    time-to-live: 0
考试又开始通过了