[Kotlin] Unit testing throws exception

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import java.util.*

class AgeCalculation() {
    fun getAge(dob: Calendar): Int {
        val today = Calendar.getInstance()
        if (dob.timeInMillis > today.timeInMillis) throw IllegalAccessException()
        val years = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR)
        return if (dob.get(Calendar.DAY_OF_YEAR) > today.get(Calendar.DAY_OF_YEAR)) {
            years - 1
        } else {
            years
        }
    }
}

class AgeCalculationTest() {

    @Test
    fun testForException() {
        val date = Calendar.getInstance()
        date.add(Calendar.DAY_OF_YEAR, 10)
        Assertions.assertThrows(java.lang.IllegalAccessException::class.java) {
            AgeCalculation().getAge(date)
        }
    }
}

 

posted @ 2020-11-01 22:00  Zhentiw  阅读(154)  评论(0)    收藏  举报