Test expected exceptions in Kotlin

后端 未结 11 974
我寻月下人不归
我寻月下人不归 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: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 {  1 / 0 }
        }
    }
    

提交回复
热议问题