Test expected exceptions in Kotlin

后端 未结 11 760
我寻月下人不归
我寻月下人不归 2021-02-02 04:45

In Java, the programmer can specify expected exceptions for JUnit test cases like this:

@Test(expected = ArithmeticException.class)
public void omg()
{
    int bl         


        
相关标签:
11条回答
  • 2021-02-02 05:12

    Nobody mentioned that assertFailsWith() returns the value and you can check exception attributes:

    @Test
    fun `my test`() {
            val exception = assertFailsWith<MyException> {method()}
            assertThat(exception.message, equalTo("oops!"))
        }
    }
    
    0 讨论(0)
  • 2021-02-02 05:13

    JUnit5 has kotlin support built in.

    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 }
        }
    }
    
    0 讨论(0)
  • 2021-02-02 05:22

    The Kotlin translation of the Java example for JUnit 4.12 is:

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

    However, JUnit 4.13 introduced two assertThrows methods for finer-granular exception scopes:

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

    Both assertThrows methods return the expected exception for additional assertions:

    @Test
    fun omg() {
        // ...
        val exception = assertThrows(ArithmeticException::class.java) {
            val blackHole = 1 / 0
        }
        assertEquals("/ by zero", exception.message)
        // ...
    }
    
    0 讨论(0)
  • 2021-02-02 05:22

    Assert extension that verifies the exception class and also if the error message match.

    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}")
    

    }

    for example:

    assertThrows<IllegalStateException>({
            throw IllegalStateException("fake error message")
        }, "fake error message")
    
    0 讨论(0)
  • 2021-02-02 05:24

    Kotlin has its own test helper package that can help to do this kind of unittest.

    Your test can be very expressive by use assertFailWith:

    @Test
    fun test_arithmethic() {
        assertFailsWith<ArithmeticException> {
            omg()
        }
    }
    
    0 讨论(0)
  • 2021-02-02 05:24

    You can use KotlinTest for this.

    In your test, you can wrap arbitary code with a shouldThrow block:

    shouldThrow<ArithmeticException> {
      // code in here that you expect to throw a ArithmeticException
    }
    
    0 讨论(0)
提交回复
热议问题