[unit-testing] Kotlin에서 예상되는 예외 테스트

Java에서 프로그래머는 다음과 같이 JUnit 테스트 케이스에 대해 예상되는 예외를 지정할 수 있습니다.

@Test(expected = ArithmeticException.class)
public void omg()
{
    int blackHole = 1 / 0;
}

Kotlin에서 어떻게해야하나요? 두 가지 구문 변형을 시도했지만 모두 작동하지 않았습니다.

import org.junit.Test

// ...

@Test(expected = ArithmeticException) fun omg()
    Please specify constructor invocation;
    classifier 'ArithmeticException' does not have a companion object

@Test(expected = ArithmeticException.class) fun omg()
                            name expected ^
                                            ^ expected ')'



답변

JUnit 4.12 용 Java 예제의 Kotlin 번역 은 다음과 같습니다.

@Test(expected = ArithmeticException::class)
fun omg() {
    val blackHole = 1 / 0
}

그러나 JUnit 4.13 assertThrows 세분화 된 예외 범위를위한 두 가지 방법을 도입했습니다 .

@Test
fun omg() {
    // ...
    assertThrows(ArithmeticException::class.java) {
        val blackHole = 1 / 0
    }
    // ...
}

assertThrows메서드 모두 추가 어설 션에 대해 예상되는 예외를 반환합니다.

@Test
fun omg() {
    // ...
    val exception = assertThrows(ArithmeticException::class.java) {
        val blackHole = 1 / 0
    }
    assertEquals("/ by zero", exception.message)
    // ...
}


답변

Kotlin에는 이러한 종류 의 단위 테스트 를 수행하는 데 도움이 될 수 있는 자체 테스트 도우미 패키지 가 있습니다.

테스트는 다음과 같이 사용하여 매우 표현력이 있습니다 assertFailWith.

@Test
fun test_arithmethic() {
    assertFailsWith<ArithmeticException> {
        omg()
    }
}


답변

@Test(expected = ArithmeticException::class).NET과 같은 Kotlin의 라이브러리 메소드 중 하나를 사용 하거나 더 나은 방법을 사용할 수 있습니다 failsWith().

수정 된 제네릭과 다음과 같은 도우미 메서드를 사용하여 더 짧게 만들 수 있습니다.

inline fun <reified T : Throwable> failsWithX(noinline block: () -> Any) {
    kotlin.test.failsWith(javaClass<T>(), block)
}

그리고 주석을 사용한 예 :

@Test(expected = ArithmeticException::class)
fun omg() {

}


답변

이를 위해 KotlinTest 를 사용할 수 있습니다 .

테스트에서 shouldThrow 블록으로 임의 코드를 래핑 할 수 있습니다.

shouldThrow<ArithmeticException> {
  // code in here that you expect to throw a ArithmeticException
}


답변

JUnit5에는 kotlin 지원이 내장되어 있습니다.

import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows

class MyTests {
    @Test
    fun `division by zero -- should throw ArithmeticException`() {
        assertThrows<ArithmeticException> {  1 / 0 }
    }
}


답변

kotlin.test 패키지와 함께 제네릭을 사용할 수도 있습니다.

import kotlin.test.assertFailsWith

@Test
fun testFunction() {
    assertFailsWith<MyException> {
         // The code that will throw MyException
    }
}


답변

예외 클래스와 오류 메시지가 일치하는지 확인하는 Assert 확장입니다.

inline fun <reified T : Exception> assertThrows(runnable: () -> Any?, message: String?) {
try {
    runnable.invoke()
} catch (e: Throwable) {
    if (e is T) {
        message?.let {
            Assert.assertEquals(it, "${e.message}")
        }
        return
    }
    Assert.fail("expected ${T::class.qualifiedName} but caught " +
            "${e::class.qualifiedName} instead")
}
Assert.fail("expected ${T::class.qualifiedName}")

}

예를 들면 :

assertThrows<IllegalStateException>({
        throw IllegalStateException("fake error message")
    }, "fake error message")