Created
July 24, 2022 10:54
-
-
Save nobel6018/b6a0d17dd66091c4d640575e0c114ae4 to your computer and use it in GitHub Desktop.
kotlin test exception by JUnit5 and AssertJ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://v3.leedo.me/devs/68