Недавно я обновил загрузочный проект Spring с 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 200. То же самое произойдет, если порядок будет обратным, поэтому
where:
thirdPartyServerResponse | expectedHealth
HttpStatus.SERVICE_UNAVAILABLE | [status: 'DOWN', thirdPartyServer: 'DOWN']
HttpStatus.OK | [status: 'UP', thirdPartyServer: 'UP']
На этот раз он терпит неудачу с 503 503. Если я добавлю строку Thread.sleep перед фактическим вызовом REST, подобным этому
when:
Thread.sleep(1000)
def response = RestAssured.given().get('/health')
то это проходит каждый раз! Итак, похоже, что Spring внес некоторые изменения в MockRestServiceServer
и что ему нужно некоторое время для настройки макета (возможно, теперь он выполняется в отдельном потоке).
У кого-нибудь была похожая проблема? Как обойти это исправление Thread.sleep и чем объясняется такое поведение?