Sling Academy
Home/Kotlin/How to Use Parameterized Tests in Kotlin with JUnit

How to Use Parameterized Tests in Kotlin with JUnit

Last updated: December 01, 2024

Writing tests is a crucial part of developing any software application. They help ensure that your code remains reliable over time. A powerful feature provided by testing libraries like JUnit is parameterized tests. These allow you to run the same test method with different inputs. Implementing parameterized tests in Kotlin using JUnit is straightforward, and it increases the robustness and reliability of your test suite.

Getting Started with JUnit and Kotlin

To start using parameterized tests in Kotlin, you need JUnit 5 (also known as JUnit Jupiter). If you are using Gradle, your build.gradle.kts file should include:


dependencies {
    testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.1")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.1")
}

// Ensure tests are using JUnit Platform

tasks.test {
    useJUnitPlatform()
}

Once you have set up JUnit in your Kotlin project, you are ready to write parameterized tests.

Writing Parameterized Tests

Parameterized tests in JUnit are supported through the @ParameterizedTest annotation. They also require a source that provides the arguments. JUnit offers several sources, like @ValueSource, @EnumSource, @CsvSource, and more. Let's look at a simple example using @ValueSource.


import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import kotlin.test.assertTrue

class ExampleTests {

    @ParameterizedTest
    @ValueSource(strings = ["racecar", "radar", "level"])
    fun testIsPalindrome(word: String) {
        assertTrue { word == word.reversed() }
    }
}

In the example above, the test method testIsPalindrome will run three times with different string inputs. It checks if each word is a palindrome.

Using @CsvSource for Multiple Arguments

Sometimes you might need to provide multiple arguments to your test function. @CsvSource allows you to specify comma-separated values. Here’s how it works:


import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource
import kotlin.test.assertEquals

class MathTests {

    @ParameterizedTest
    @CsvSource(
        "1, 1, 2",
        "2, 3, 5",
        "4, 5, 9"
    )
    fun testAddition(first: Int, second: Int, expected: Int) {
        assertEquals(expected, first + second)
    }
}

This test will run for each line specified in the @CsvSource, each time with a different combination of numbers. This is useful for testing functions with multiple inputs.

Advanced Parameter Sources

For more complex scenarios, you can use @MethodSource which lets you define a static method that generates the arguments dynamically. Here's an example:


import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
import kotlin.test.assertTrue

class CustomTests {

    companion object {
        @JvmStatic
        fun parameterProvider() = listOf(
            "kayak", "deified", "rotor"
        )
    }

    @ParameterizedTest
    @MethodSource("parameterProvider")
    fun testIsPalindromeAdvanced(word: String) {
        assertTrue { word == word.reversed() }
    }
}

The parameterProvider method supplies the input for each test iteration without hardcoding the values in the annotation itself.

Conclusion

Parameterized tests in Kotlin using JUnit improve the coverage and clarity of your test cases by avoiding repeated code patterns. This approach allows you to easily test your functions with multiple sets of data and quickly identify edge cases. With the different sources offered by JUnit, you have the flexibility to create concise and powerful test cases.

Incorporate parameterized tests into your Kotlin testing setup to make your tests more dynamic and data-driven, while reducing redundancy and improving maintainability.

Next Article: Using Assertions for List and Collection Tests in Kotlin

Previous Article: Testing Class Methods and Properties in Kotlin

Series: Testing in Kotlin

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin