Skip to content

Instantly share code, notes, and snippets.

@nobel6018
Created July 24, 2022 10:54
Show Gist options
  • Save nobel6018/b6a0d17dd66091c4d640575e0c114ae4 to your computer and use it in GitHub Desktop.
Save nobel6018/b6a0d17dd66091c4d640575e0c114ae4 to your computer and use it in GitHub Desktop.
kotlin test exception by JUnit5 and AssertJ
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.junit.jupiter.api.Test
@Test
fun `test exception1 - AssertionJ`() {
assertThatThrownBy {
// lambda 부분입니다. 실행할 코드를 작성합니다.
val list = listOf(1, 2, 3, 4)
list.get(100)
}
.isExactlyInstanceOf(ArrayIndexOutOfBoundsException::class.java)
.isInstanceOf(Exception::class.java) // 부모 클래스 타입도 체크할 수 있습니다.
.hasMessage("Index 100 out of bounds for length 4")
}
@Test
fun `test exception2 - AssertionJ`() {
assertThatThrownBy { throw RuntimeException("Occurred run time exception") }
.isExactlyInstanceOf(RuntimeException::class.java)
.isInstanceOf(Exception::class.java) // 부모 클래스 타입도 체크할 수 있습니다.
.hasMessage("Occurred run time exception")
}
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.assertThrows
@Test
fun `test exception1 - JUnit5`() {
val assertion = assertThrows<ArrayIndexOutOfBoundsException> {
// lambda 부분입니다. 실행할 코드를 작성합니다.
val list = listOf(1, 2, 3, 4)
list.get(100)
}
assertEquals("Index 100 out of bounds for length 4", assertion.message)
}
@Test
fun `test exception2 - JUnit5`() {
val assertion = assertThrows<RuntimeException> { throw RuntimeException("Occurred run time exception") }
assertEquals("Occurred run time exception", assertion.message)
}
@nobel6018
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment