Skip to content

Instantly share code, notes, and snippets.

@rossharper
Created February 20, 2016 21:51
Show Gist options
  • Save rossharper/8f6c3c169b6b5c23e12c to your computer and use it in GitHub Desktop.
Save rossharper/8f6c3c169b6b5c23e12c to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link

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!

@LorenBaker
Copy link

Thanks for this ... just what i needed to know.

@hardikc07
Copy link

hardikc07 commented Oct 1, 2020

Is it possible to add multiple test method in the same class and test them against data ?

Copy link

ghost commented Jul 28, 2021

Is it possible to add multiple test method in the same class and test them against data ?

Sure!

@rossbeazley
Copy link

holly crap, stumbled across this ;)
👋

@MamboBryan
Copy link

holly crap, stumbled across this ;) 👋

holly molly, came across this 😉

@khaledaboulezz
Copy link

Thanks, but i have a question here

After using the same code, the test runs twice with the same parameter

Any ideas how to prevent that from happening?

@rishikhaneja
Copy link

rishikhaneja commented May 13, 2024

Thanks for this snippet 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment