Helio
01/15/2021, 9:46 PMchristophsturm
01/15/2021, 10:11 PMHelio
01/15/2021, 10:20 PMprivate fun mockHttpErrorResponse(httpStatusCode: HttpStatusCode): HttpResponse {
val client = HttpClient(MockEngine) {
engine {
addHandler {
respondError(httpStatusCode)
}
}
}
return runBlocking { client.get() }
}
Helio
01/15/2021, 10:22 PM@Test
fun bbcPipelinesBuildInfoThrowClientException(): Unit = withTestApplication(Application::module) {
val compliantToken = TokenatorTokenModelEntityBuilder.BUILD_COMPLIANT_TOKEN
declareMock<BbcPipelineBuildInfoServiceImpl> {
coEvery {
getBitbucketPipelineBuildInfo(any(), any(), any(), any(), any(), any())
} throws ClientRequestException(mockHttpErrorResponse(HttpStatusCode.Unauthorized))
}
declareMock<TokenatorServiceImpl> { coEvery { generateToken(any(), any()) } returns compliantToken }
with(handleRequest(<http://HttpMethod.Post|HttpMethod.Post>, "token/build") {
SlauthHeadersBuilder.BUILD_PRINCIPAL_HEADERS.forEach { (key, value) -> addHeader(key, value) }
}) {
assertThat(HttpStatusCode.InternalServerError).isEqualTo(response.status())
}
}
This was working fine with ktor 1.4.0, 1.4.2, 1.4.3. But it doesn’t work with 1.5.0.
To be more specific, debugging the test, it doesn’t even reach the following line,
declareMock<TokenatorServiceImpl> { coEvery { generateToken(any(), any()) } returns compliantToken }
Which, is the exactly line after trying to mock the HttpResponse.christophsturm
01/15/2021, 10:40 PMHelio
01/15/2021, 10:45 PMHelio
01/15/2021, 10:45 PMrunBlocking {client.get() }
throws the exception, instead of just “create” the response and mock it silently.christophsturm
01/15/2021, 11:02 PMHelio
01/15/2021, 11:05 PMchristophsturm
01/15/2021, 11:17 PM