Kotlin

Parameterized Tests with JUnit 5

admin by @admin ADMIN
Jun 17, 2026
Jun 1, 2026
Public
0 0 up · 0 down Sign in to vote
JUnit 5 + Kotlin: `@ParameterizedTest` runs the same test body with multiple input rows. `@CsvSource` for inline data, `@MethodSource` for complex args.
Kotlin
Raw
// build.gradle.kts:
//   testImplementation("org.junit.jupiter:junit-jupiter:5.10.+")
//   testImplementation("org.junit.jupiter:junit-jupiter-params:5.10.+")

import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.*
import kotlin.test.Test

fun slugify(s: String): String =
    s.lowercase().replace(Regex("[^a-z0-9]+"), "-").trim('-')

class SlugifyTest {

    @ParameterizedTest(name = "[{index}] slugify({0}) → {1}")
    @CsvSource(
        "Hello World,    hello-world",
        "  Trim Me  ,    trim-me",
        "PHP 8.3,         php-8-3",
        "double--dashes,  double-dashes",
    )
    fun slugify_examples(input: String, expected: String) {
        assertEquals(expected, slugify(input))
    }

    @ParameterizedTest
    @ValueSource(strings = ["a", "b", "c"])
    fun every_value_is_uppercased(s: String) {
        assertEquals(s.uppercase(), s.uppercase())
    }
}
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.