Kotest 예외(Exceptions)

Kotlin의 예외를 제외하는 단언문에 대해서 설명한다.

Exceptions

주어진 코드 블록이 예외를 발생시킨다고 주장하려면, shouldThrow 함수를 사용할 수 있다.

예를 들면,

shouldThrow<IllegalAccessException> {
    // code in here that you expect to throw an IllegalAccessException
}

발생한 예외를 확인할 수도 있다:

val exception = shouldThrow<IllegalAccessException> {
    // code in here that you expect to throw an IllegalAccessException
}
exception.message should startWith("Something went wrong")

특정 유형의 예외가 발생하는지 테스트하려면 shouldThrowExactly<E>를 사용한다. 예를 들어, 다음 블록은 FileNotFoundExceptionIOException에서 확장되었지만 IOException은 잡아내지 못한다.

val exception = shouldThrowExactly<FileNotFoundException> {
    // test here
}

단순히 유형에 관계없이 모든 예외가 발생했는지 테스트하려면 shouldThrowAny를 사용한다.

val exception = shouldThrowAny {
    // test here can throw any type of Throwable!
}

참조




최종 수정 : 2024-04-21