Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parameterized JUnit4 test example in Kotlin
@RunWith(Parameterized::class)
class KotlinTest(val paramOne: Int, val paramTwo: String) {
companion object {
@JvmStatic
@Parameterized.Parameters
fun data() : Collection<Array<Any>> {
return listOf(
arrayOf(1, "I"), // First test: (paramOne = 1, paramTwo = "I")
arrayOf(1999, "MCMXCIX") // Second test: (paramOne = 1999, paramTwo = "MCMXCIX")
)
}
}
@Test
fun shouldReturnExpectedRomanForArabic() {
assertThat(RomanNumeralGenerator().arabicToRoman(paramOne), equalTo(paramTwo));
}
}
@code-schreiber

This comment has been minimized.

Copy link

commented Aug 2, 2018

Thanks for this! It gave me the right direction.
Here is my variation of it using Kotlin's expression body:

@RunWith(Parameterized::class)
class KotlinParameterizedTest(private val paramOne: Int, private val paramTwo: String) {

    private val underTest = RomanNumeralGenerator()

    companion object {
        @JvmStatic
        @Parameterized.Parameters
        fun data() = listOf(
                arrayOf(1, "I"),         // First test:  (paramOne = 1, paramTwo = "I")
                arrayOf(1999, "MCMXCIX") // Second test: (paramOne = 1999, paramTwo = "MCMXCIX")
        )
    }

    @Test
    fun shouldReturnExpectedRomanForArabic() {
        assertThat(underTest.arabicToRoman(paramOne), equalTo(paramTwo));
    }

}
@tieorange

This comment has been minimized.

Copy link

commented Oct 9, 2019

Thanks for this! It gave me the right direction.
Here is my variation of it using Kotlin's expression body:

@RunWith(Parameterized::class)
class KotlinParameterizedTest(private val paramOne: Int, private val paramTwo: String) {

    private val underTest = RomanNumeralGenerator()

    companion object {
        @JvmStatic
        @Parameterized.Parameters
        fun data() = listOf(
                arrayOf(1, "I"),         // First test:  (paramOne = 1, paramTwo = "I")
                arrayOf(1999, "MCMXCIX") // Second test: (paramOne = 1999, paramTwo = "MCMXCIX")
        )
    }

    @Test
    fun shouldReturnExpectedRomanForArabic() {
        assertThat(underTest.arabicToRoman(paramOne), equalTo(paramTwo));
    }

}

Looks good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.