Test expected exceptions in Kotlin

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

    Firt steps is to add (expected = YourException::class) in test annotation

    @Test(expected = YourException::class)
    

    Second step is to add this function

    private fun throwException(): Boolean = throw YourException()
    

    Finally you will have something like this:

    @Test(expected = ArithmeticException::class)
    fun `get query error from assets`() {
        //Given
        val error = "ArithmeticException"
    
        //When
        throwException()
        val result =  omg()
    
        //Then
        Assert.assertEquals(result, error)
    }
    private fun throwException(): Boolean = throw ArithmeticException()
    

提交回复
热议问题