autoparams-kotlin
autoparams-kotlin
is an extension of AutoParams that adds Kotlin-specific support for test data generation. It helps reduce boilerplate in Kotlin tests by generating values in a way that aligns with Kotlin’s language features.
Install
Maven
For Maven, you can add the following dependency to your pom.xml
:
<dependency>
<groupId>io.github.autoparams</groupId>
<artifactId>autoparams-kotlin</artifactId>
<version>10.0.0</version>
</dependency>
Gradle (Groovy)
For Gradle Groovy DSL, use:
testImplementation 'io.github.autoparams:autoparams-kotlin:10.0.0'
Gradle (Kotlin)
For Gradle Kotlin DSL, use:
testImplementation("io.github.autoparams:autoparams-kotlin:10.0.0")
@AutoKotlinParams
Annotation
Consider the following Kotlin data class:
- Kotlin
data class Point(val x: Int = 0, val y: Int = 0)
Using default values in tests can result in limited test coverage. The @AutoKotlinParams
annotation enables AutoParams to provide randomized arguments for Kotlin test methods, ensuring more varied and meaningful input.
Here's an example:
- Kotlin
@Test
@AutoKotlinParams
fun testMethod(point: Point) {
assertThat(point.x).isNotEqualTo(0)
assertThat(point.y).isNotEqualTo(0)
}
In this test, the point
parameter is automatically initialized with non-default, randomly generated values—allowing the test to cover a broader range of scenarios without requiring manual setup.