Skip to main content
Version: v1.2.x

Features

The Kotlin plugin helps you take full advantage of Kotlin's concise, safe, and pragmatic nature when generating test fixtures.

What the Kotlin Plugin provides​

  • PrimaryConstructorArbitraryIntrospector as the default introspector — generates Kotlin classes using their primary constructor
  • Extension functions — giveMeOne<T>(), giveMeKotlinBuilder<T>() for idiomatic Kotlin usage
  • Kotlin DSL Exp — type-safe property references (KotlinClass::field) instead of string-based paths
  • Kotlin instantiateBy DSL — choose which constructor or factory method to use

Dependencies​

:::tip Recommended Use fixture-monkey-starter-kotlin for new projects. It includes fixture-monkey-starter and fixture-monkey-jakarta-validation out of the box. :::

Gradle​

testImplementation("com.navercorp.fixturemonkey:fixture-monkey-starter-kotlin:1.2.3")

Maven​

<dependency>
<groupId>com.navercorp.fixturemonkey</groupId>
<artifactId>fixture-monkey-starter-kotlin</artifactId>
<version>1.2.3</version>
<scope>test</scope>
</dependency>

If you only need the core Kotlin support without the starter bundle:

testImplementation("com.navercorp.fixturemonkey:fixture-monkey-kotlin:1.2.3")

Plugin Setup​

val fixtureMonkey = FixtureMonkey.builder()
.plugin(KotlinPlugin())
.build()

Quick Example​

KotlinquickExample()
KotlinPluginFeaturesTest.kt
// given
val product: Product = fixtureMonkey.giveMeOne()

// then
then(product).isNotNull

With customization using type-safe property references:

KotlincustomizeWithTypeSafeReferences()
KotlinPluginFeaturesTest.kt
// when
val custom = fixtureMonkey.giveMeKotlinBuilder<Product>()
.setExp(Product::name, "Fixture Monkey")
.setExp(Product::price, 29_900L)
.sizeExp(Product::options, 3)
.sample()

// then
then(custom.name).isEqualTo("Fixture Monkey")
then(custom.price).isEqualTo(29_900L)
then(custom.options).hasSize(3)

Extension Functions​

The Kotlin plugin provides extension functions that leverage Kotlin's reified type parameters, so you don't need to pass Class<T> explicitly:

Extension FunctionDescription
giveMeOne<T>()Generate a single random instance
giveMeBuilder<T>()Get an ArbitraryBuilder<T> (string-based API)
giveMeKotlinBuilder<T>()Get a Kotlin-aware builder with setExp, sizeExp etc.
giveMe<T>(count)Generate a stream of random instances
// No need for Product::class.java
val product: Product = fixtureMonkey.giveMeOne()
val products: List<Product> = fixtureMonkey.giveMe<Product>(5).toList()
val builder = fixtureMonkey.giveMeKotlinBuilder<Product>()

Type-Safe Property References (Kotlin DSL Exp)​

Instead of string-based path expressions like set("name", value), use Kotlin property references for compile-time safety:

// String-based (works but no compile-time check)
builder.set("name", "Fixture Monkey")

// Type-safe (compile error if property doesn't exist)
builder.setExp(Product::name, "Fixture Monkey")

For nested properties, use the into infix function:

KotlinnestedPropertyReference()
KotlinPluginFeaturesTest.kt
// when
val order = fixtureMonkey.giveMeKotlinBuilder<Order>()
.setExp(Order::product into Product::name, "Fixture Monkey")
.sample()

// then
then(order.product.name).isEqualTo("Fixture Monkey")

For Java classes (getter-based), use setExpGetter and intoGetter:

builder.setExpGetter(JavaProduct::getName, "Fixture Monkey")
builder.setExpGetter(
JavaOrder::getProduct intoGetter JavaProduct::getName,
"Fixture Monkey"
)

See Kotlin DSL Exp for the full reference.

Introspectors for Kotlin​

The Kotlin plugin sets PrimaryConstructorArbitraryIntrospector as the default, which creates objects using Kotlin's primary constructor.

For projects that mix Kotlin and Java classes, use KotlinAndJavaCompositeArbitraryIntrospector:

val fixtureMonkey = FixtureMonkey.builder()
.plugin(KotlinPlugin())
.objectIntrospector(KotlinAndJavaCompositeArbitraryIntrospector())
.build()

See Introspectors for Kotlin for details.