Jorge Bo
04/18/2023, 10:22 PM@Test
fun `can update passengers name 200 response`() = runTest {
fun successUseCase(): UseCase<UpdatePassengersData, BusinessError, Unit> =
object : UseCase<UpdatePassengersData, BusinessError, Unit> {
override suspend fun invoke(command: Command<UpdatePassengersData>): Either<BusinessError, Unit> {
return Either.Right(Unit)
}
}
withTestApplication(withBookingTestApplication(successUseCase())) {
with(
handleRequest(<http://HttpMethod.Post|HttpMethod.Post>, "/updatePassengers") {
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
addHeader(HttpHeaders.Accept, ContentType.Application.Json.toString())
setBody(UPDATE_PASSENGER)
}
) {
Assertions.assertEquals(HttpStatusCode.NoContent, response.status())
}
}
}
private fun withBookingTestApplication(useCase: UseCase<UpdatePassengersData, BusinessError, Unit>): Application.() -> Unit {
return withDefaultTestApplication {
registerUpdatePassengersRoute(useCase, TestCalendar)
}
}
Aleksei Tirman [JB]
04/19/2023, 7:40 AMJorge Bo
04/19/2023, 12:41 PM@Test
fun `can update passengers name 200 response`() = testApplication {
environment {
config = ApplicationConfig("application-test.conf")
}
application {
installServerContentNegotiation()
installIgnoreTrailingSlash()
installKompendium()
installRfcProblems()
registerHealthRoutes()
registerOASRoutes()
registerUpdatePassengersRoute(
object : UseCase<UpdatePassengersData, BusinessError, Unit> {
override suspend fun invoke(command: Command<UpdatePassengersData>): Either<BusinessError, Unit> {
return Either.Right(Unit)
}
},
TestCalendar
)
}
val response = <http://client.post|client.post>("/updatePassengers") {
header(HttpHeaders.ContentType, ContentType.Application.Json.toString())
header(HttpHeaders.Accept, ContentType.Application.Json.toString())
setBody(UPDATE_PASSENGER)
}
assertEquals(HttpStatusCode.NoContent, response.status)
assertEquals("", response.bodyAsText())
}
and application-test.conf
ktor {
deployment {
port = 8080
runningLimit = 150
tcpKeepAlive = false
requestQueueLimit = 1500
responseWriteTimeoutSeconds = 1
connectionGroupSize = 2
workerGroupSize = 8
callGroupSize = 8
}
/* Remove this block to avoid loading the main module
application {
modules = [ ApplicationKt.module ]
}
*/
}
Jorge Bo
04/19/2023, 12:49 PMfun Application.module() {
<http://logger.info|logger.info>("Starting main application...")
installServerContentNegotiation()
installIgnoreTrailingSlash()
installKompendium()
installRfcProblems()
registerHealthRoutes()
registerOASRoutes()
//register all the routes of my service
.....
}
so in order to avoid loading all the routes for my tests and install the necessary plugins i had to copy and paste the plugin conf which i found not correct as it might not reflect the real configuration.
environment {
config = ApplicationConfig("application-test.conf")
}
application {
installServerContentNegotiation()
installIgnoreTrailingSlash()
installKompendium()
installRfcProblems()
registerHealthRoutes()
registerOASRoutes()
My question is , is it correct to test this way? i would like to load the main plugin configuration but choose which route to test so that i can mock individually.Jorge Bo
04/19/2023, 6:14 PM