All Downloads are FREE. Search and download functionalities are using the official Maven repository.

autoparams.kotlin.KotlinClassCustomizer.kt Maven / Gradle / Ivy

Go to download

A Kotlin-specific addition to the AutoParams library, allowing for the automated generation of random parameters for unit testing in Kotlin. This extension is designed to leverage Kotlin's unique features for more powerful and flexible test data generation.

There is a newer version: 8.3.0
Show newest version
package autoparams.kotlin

import java.lang.reflect.ParameterizedType
import autoparams.customization.Customizer
import autoparams.generator.ObjectContainer
import autoparams.generator.ObjectGenerator
import org.junit.jupiter.params.converter.ArgumentConverter
import kotlin.reflect.KClass

internal class KotlinClassCustomizer : Customizer {

    override fun customize(generator: ObjectGenerator): ObjectGenerator {
        return ObjectGenerator { query, context ->
            when (query.type) {
                is ParameterizedType ->
                    when ((query.type as ParameterizedType).rawType) {
                        KClass::class.java -> ObjectContainer(String::class)
                        else -> generator.generate(query, context)
                    }

                ArgumentConverter::class.java -> generator
                    .generate(ArgumentConverter::class.java, context)
                    .process { value ->
                        KotlinClassConverter(value as ArgumentConverter)
                    }

                else -> generator.generate(query, context)
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy