Skip to content

HelloCuriosity/model-forge

Repository files navigation

Model Forge πŸ”₯πŸ”¨

Build Status codecov License Contributor Covenant Sonatype Nexus (Snapshots) Awesome Kotlin Badge

About

Model Forge is a library to automate model generation for automated testing:

  • unit
  • integration
  • etc.

Getting Started

Gradle Setup

Kotlin
dependencies {
    testImplementation("io.github.hellocuriosity:model-forge:1.5.1")
}
Groovy
dependencies {
    testImplementation 'io.github.hellocuriosity:model-forge:1.5.1'
}

Feeling Adventurous πŸ’₯

If you're feeling adventurous you can be on the cutting edge and test a snapshot:

Kotlin
repositories {
    maven(url = "https://s01.oss.sonatype.org/content/repositories/snapshots/")
}

dependencies {
    testImplementation("io.github.hellocuriosity:model-forge:1.5.1.xx-SNAPSHOT")
}
Groovy
repositories {
    maven { url 'https://s01.oss.sonatype.org/content/repositories/snapshots/' }
}

dependencies {
    testImplementation 'io.github.hellocuriosity:model-forge:1.5.1.xx-SNAPSHOT'
}

Define your model

data class Employee(
    val id: Long,
    val name: String,
    val dob: Instant,
)

Generate model

val forge = ModelForge()
val testObject = forge.build<Employee>()

or by delegating

val testObject: Employee by forgery()

You can create different sized lists by specifying the number of elements.

val forge = ModelForge()
val list = forge.buildList<TestObject>(3)

or by delegating

val testObjects: List<TestObject> by forgeryList()

Custom Provider

While Model Forge aims to fully automate model generation, you may run into an instance where you need to customize your data. This is easily achievable by defining a custom provider and adding it to the forge.

Define your provider

val testProvider: Provider<TestObject> = Provider {
    Employee(
        id = 15L,
        name = "Josh",
        dob = Instant.ofEpochMilli(1315260000000)
    )
}

Add your provider to the forge

forge.addProvider(TestObject.class,testProvider);

or Kotlin

forge.addProvider(testProvider)

Inline your provider(s)

Alternatively you can add your forgery providers inline

val forge = ModelForge().apply {
    addProvider {
        Employee(
            id = 2L,
            name = "Hendrik",
            dob = Instant.ofEpochMilli(1574486400000)
        )
    }
}
val employee by forgery<Employee>(forge)

Supported Types

Model Forge currently supports the auto generation for the following types:

Types

  • Boolean
  • Byte
  • Calendar
  • Char
  • Date
  • Double
  • Enums
  • File
  • Float
  • Int
  • Instant
  • Long
  • Short
  • String
  • UByte
  • UInt
  • ULong
  • UShort
  • UUID

Collections

  • List
  • Map
  • Set

Can't find your data type? Feel free to create a pull request or open an issue πŸͺ‚

Contributing

Thanks for showing your interest in wanting to improve Model Forge. Before you get started take a look at our code of conduct and contribution guides πŸ™Œ

Contributors

If you contribute to Model Forge, please feel free to add yourself to the list!

  • Kyle Roe - Maintainer
  • Adriaan Duz - Contributor
    • Kotlin class definitions
    • Forgery and forgeries property delegate
    • Reified inline extension functions
  • Alicja Kozikowska - Contributor
    • Fix StringProvider treatment of invalid wordCount