Quick testing tips #13


Test exceptions with runCatching




import io.kotest.core.spec.style.StringSpec
import org.junit.jupiter.api.Assertions.assertNotNull

class Test13 : StringSpec({
  "it should throw exception" {
    val systemUnderTest = SystemUnderTest()
    val exception = kotlin.runCatching {
      systemUnderTest.doStuff()
    }.exceptionOrNull()

    assertNotNull(exception)
  }
})



Need to check system under test against exceptions? Use runCatching to resolve exception from given block and then make regular assertion.



Read next:


Loading ...
Failed to load data.