Skip to content

The fixture-ktx is a Kotlin utility for generating mock data, aiding in unit testing by providing realistic test cases. It supports diverse property types, ensuring comprehensive test coverage.

License

Notifications You must be signed in to change notification settings

heroesofcode/fixture-ktx

Repository files navigation

fixture-ktx

The fixture-ktx is a tool that helps generate class instances with random data to facilitate unit testing. It supports a variety of property types, including strings, numbers, booleans, enums, sealed classes, and exceptions.

Installation

Add it in your root settings.gradle or build.gradle at the end of repositories:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven("https://jitpack.io")
    }
}

Add the Fixture Library dependency to your build configuration file. For example, if you're using Gradle, add the following to your build.gradle file:

dependencies {
    implementation 'com.github.heroesofcode:fixture-ktx:$version'
}

Usage

Generate a Single Instance

To generate an instance of a class with random data, use the fixtureOf method.

import io.heroesofcode.Fixture.fixtureOf

val user = fixtureOf<User>()

Generating a List of Instances

To generate a list of class instances with random data, use the fixtureListOf method.

import io.heroesofcode.Fixture.fixtureListOf

val users = fixtureListOf<User>()

Example Class

Let's consider the following User class as an example:

data class User(
    val name: String,
    val age: Int,
    val email: String,
    val isActive: Boolean,
    val accountBalance: Double
)

Example Usage

fun main() {
    val user: User = fixtureOf()
    println(user)

    val users: List<User> = fixtureListOf(size = 5)
    users.forEach { println(it) }
}

Supported Property Types

The fixture-ktx currently supports the following property types:

  • String: Generates a random string.
  • Int: Generates a random integer.
  • Boolean: Generates a random boolean value.
  • Double: Generates a random double value.
  • Float: Generates a random float value.
  • Enum: Randomly selects a value from an enumeration.
  • Sealed Classes: Randomly selects a subclass of a sealed class.
  • Exceptions: Generates instances of exceptions with a default message.
  • List: Generates a list of random values of a specified type.
  • Array: Generates an array of random values of a specified type.
  • Set: Generates a set of random values of a specified type.

Generate Data

The library leverages the Kotlin-Faker library to generate realistic fake data, providing a closer match to real-world scenarios.

Contributions

Contributions are welcome! Feel free to open an issue or submit a pull request on the project's repository.

About

The fixture-ktx is a Kotlin utility for generating mock data, aiding in unit testing by providing realistic test cases. It supports diverse property types, ensuring comprehensive test coverage.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages