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.
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'
}
To generate an instance of a class with random data, use the fixtureOf method.
import io.heroesofcode.Fixture.fixtureOf
val user = fixtureOf<User>()
To generate a list of class instances with random data, use the fixtureListOf method.
import io.heroesofcode.Fixture.fixtureListOf
val users = fixtureListOf<User>()
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
)
fun main() {
val user: User = fixtureOf()
println(user)
val users: List<User> = fixtureListOf(size = 5)
users.forEach { println(it) }
}
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.
The library leverages the Kotlin-Faker library to generate realistic fake data, providing a closer match to real-world scenarios.
Contributions are welcome! Feel free to open an issue or submit a pull request on the project's repository.