I want to cover a case via test when exception is thrown. I've tried passing an incorrect input but still no luck.
In Kotest - can we explicitly throw exception when a function is called?
I couldn't find any documentation in Kotest Doc to cover this scenario:
Main.kt
parseEvent(input).forEach { event ->
try {
eventsProcessor(event)
} catch (ex: Exception) {
log.error { ex }
batchItemFailures.add(SQSBatchResponse.BatchItemFailure(event.msgId))
}
}
private fun eventsProcessor(event: Event<*>) {
try {
when (event.type) {
"xyz" -> dailyprocess()
else -> log.warn { "Unknown event type: ${event.type}" }
}
} catch (ex: Exception) {
log.error { ex }
throw ex
}
}
Test.kt
describe("Event parsing") {
context("when event is just a map") {
val event = mapOf(
"Records" to listOf(
mapOf("body" to "jsonBody1")))
it("parses and process event") {
handler.handleRequest(event, createTestContext())
val exception = shouldThrow<Exception> {
dailyprocess(Instant.now())
}
}
}
}