Created
February 20, 2016 21:51
-
-
Save rossharper/8f6c3c169b6b5c23e12c to your computer and use it in GitHub Desktop.
Parameterized JUnit4 test example in Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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)); | |
} | |
} |
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!
Thanks for this ... just what i needed to know.
Is it possible to add multiple test method in the same class and test them against data ?
Is it possible to add multiple test method in the same class and test them against data ?
Sure!
holly crap, stumbled across this ;)
👋
holly crap, stumbled across this ;) 👋
holly molly, came across this 😉
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?
Thanks for this snippet 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this! It gave me the right direction.
Here is my variation of it using Kotlin's expression body: