Test expected exceptions in Kotlin

后端 未结 11 989
我寻月下人不归
我寻月下人不归 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:33

    You can use @Test(expected = ArithmeticException::class) or even better one of Kotlin's library methods like failsWith().

    You can make it even shorter by using reified generics and a helper method like this:

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

    And example using the annotation:

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

提交回复
热议问题