Back to directory
HelloCuriosity avatar
HelloCuriosity / model-forge

model-forge

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

22

Stars

6

Forks

1

Watchers

MIT

License

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

Releases

What's Changed

Update docusaurus monorepo to v3.6.1 by @renovate in #388 Update JamesIves/github-pages-deploy-action action to v4.6.9 by @renovate in #389 Update dependency gradle to v8.11 by @renova...

Oct 31, 2024

Download .zip

What's Changed

[Housekeeping] Renovate Labeling by @hopeman15 in #308 [Feature] UInt Provider by @hopeman15 in #312 [Documentation] Add UInt to Introduction by @hopeman15 in #313 [Fix] Use UIntProvid...

Feb 19, 2024

Download .zip

What's Changed

Bump http-cache-semantics from 4.1.0 to 4.1.1 in /website by @dependabot in #240 Bump actions/setup-java from 3.9.0 to 3.10.0 by @dependabot in #241 Bump webpack from 5.73.0 to 5.76.1...

What's Changed

[Documentation] Fix Version Number by @hopeman15 in #221 [Enhancement] Use SVG logo by @hopeman15 in #222 Bump actions/setup-node from 3.4.1 to 3.5.1 by @dependabot in #223 Bump action...

Nov 12, 2022

Download .zip

What's Changed

[Testing] Add Missing Set Tests by @hopeman15 in #215 Bump actions/setup-java from 3.5.1 to 3.6.0 by @dependabot in #217 Bump styfle/cancel-workflow-action from 0.10.1 to 0.11.0 by @de...