Skip to content

Instantly share code, notes, and snippets.

@fernandospr
Created April 27, 2020 19:45
Show Gist options
  • Save fernandospr/c49253e047ef530fe927539805eb5354 to your computer and use it in GitHub Desktop.
Save fernandospr/c49253e047ef530fe927539805eb5354 to your computer and use it in GitHub Desktop.
JUnit4 Parameterized Test
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
class Multiplier {
fun byTwo(value: Int): Int = value * 2
}
@RunWith(Parameterized::class)
class KotlinTest(
private val paramValue: Int,
private val expectedResult: Int
) {
companion object {
@JvmStatic
@Parameterized.Parameters
fun data() = listOf(
arrayOf(0, 0), // First test: (paramValue = 0, expectedResult = 0)
arrayOf(1, 2), // Second test: (paramValue = 1, expectedResult = 2)
arrayOf(2, 4), // Third test: (paramValue = 2, expectedResult = 4)
arrayOf(-1, -2) // Fourth test: (paramValue = -1, expectedResult = -2)
)
}
@Test
fun `Should multiply by two`() {
val multiplier = Multiplier()
val actualResult = multiplier.byTwo(paramValue)
Assert.assertEquals(expectedResult, actualResult)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment