JUnit Test for when clause in Kotlin

Viewed 425

How to write test case for below piece of code:

fun calculateResult(score: Int): String {
    return when {
        score >= 257 -> "Genius"
        score in 234..256 -> "Great"
        else -> "Good"
    }
}

Any help would be appreciated!

1 Answers

As you would test a method with many if statements. You have to write a test for every each possible return value. You call the method and give it a score and then you make an assertion.

Example:

@Test
fun test() {
   
    val test = calculateResult(258)
    
    Assertions.assertThat(test).isEqualTo("Genius")
}
Related