{"id":23266146,"url":"https://github.com/raphiz/faktory-bot","last_synced_at":"2025-08-20T22:30:33.385Z","repository":{"id":79285357,"uuid":"585202690","full_name":"raphiz/faktory-bot","owner":"raphiz","description":"Generate factories to cleanly create and maintain test data","archived":false,"fork":false,"pushed_at":"2024-06-06T05:02:55.000Z","size":351,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-06-06T15:15:59.211Z","etag":null,"topics":["factory","kotlin"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/raphiz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-01-04T15:21:31.000Z","updated_at":"2024-06-06T15:15:59.211Z","dependencies_parsed_at":null,"dependency_job_id":"c50292d2-22e5-421d-bac0-20752c95dbd1","html_url":"https://github.com/raphiz/faktory-bot","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raphiz%2Ffaktory-bot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raphiz%2Ffaktory-bot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raphiz%2Ffaktory-bot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raphiz%2Ffaktory-bot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/raphiz","download_url":"https://codeload.github.com/raphiz/faktory-bot/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230462827,"owners_count":18229862,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["factory","kotlin"],"created_at":"2024-12-19T15:52:50.554Z","updated_at":"2024-12-19T15:52:51.243Z","avatar_url":"https://github.com/raphiz.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"## faktory-bot\n\n`factory-bot` allows you to cleanly create and maintain test data. \n\nYou can use it to define your own Factories/Generators and override this logic directly when instantiating the factories in your tests.\n\n\n```kotlin\nobject UserFactory{\n    val validUser = UserSpec(\n        id = { UUID.randomUUID() },\n        name = { \"Peter\" }\n    )\n\n    val userWithAddress = validUser.copy(\n        address = { Addresses.validAddress() }\n    )\n}\n\n// in your tests\nval user = UserFactory.validUser()\nval martha = UserFactory.validUser(name = \"Martha\")\nval userWithAddr\n```\n\n`factory-bot` is intended as a clone of Ruby's fantastic [factory_bot](https://github.com/thoughtbot/factory_bot) gem for Kotlin.\n\n\n## Installation\n\n`faktory-bot` generates boilerplate code using\nthe [Kotlin Symbol Processing (KSP)](https://kotlinlang.org/docs/ksp-overview.html). Therefore, you\nneed to apply the `ksp` gradle plugin and add the following dependencies to your `build.gradle.kts`:\n\n```kotlin\nplugins {\n    kotlin(\"jvm\") version \"1.7.10\"\n    // The following version must match with the kotlin version\n    id(\"com.google.devtools.ksp\") version \"1.7.10-1.0.7\"\n}\n\ndependencies {\n    compileOnly(\"io.github.raphiz:faktory-bot-annotations:0.1.0\")\n    ksp(\"io.github.raphiz:faktory-bot-processor:0.1.0\")\n}\n\n\n// Make IDE aware of generated code, see also:\n// https://kotlinlang.org/docs/ksp-quickstart.html#make-ide-aware-of-generated-code\nkotlin {\n    sourceSets.main {\n        kotlin.srcDir(\"build/generated/ksp/main/kotlin\")\n    }\n}\n```\n\n\u003e **_NOTE:_\n**  [The implementation of KSP is tied to a specific kotlin compiler version. Users of processors (`faktory-bot` is a processor) need to bump KSP version when bumping the compiler version in their project.](https://kotlinlang.org/docs/ksp-faq.html#how-to-upgrade-ksp). `faktory-bot`\n\u003e only depends on the KSP API, and _should_ therefore work with most ksp and kotlin versions. If not,\n\u003e please [open an issue](https://github.com/raphiz/faktory-bot/issues).\n\n## Usage\n\nOnce the KSP setup is completed, the usage of `faktory-bot` is straight forward.\n\nFirst, add the `@Faktory` annotation any class, for example:\n\n```kotlin\n@Faktory\ndata class User(val id: UUID, val name: String, val address: Address?)\n```\n\n`faktory-bot` will now generate the following code for you:\n\n```kotlin\npublic data class UserSpec(\n    public val id: () -\u003e UUID,\n    public val name: () -\u003e String,\n    public val address: () -\u003e Address? = { null },\n) {\n    public operator fun invoke(\n        id: UUID = this.id(),\n        name: String = this.name(),\n        address: Address? = this.address(),\n    ): User = User(id = id, name = name, address = address,)\n\n    public fun create(\n        id: UUID = this.id(),\n        name: String = this.name(),\n        address: Address? = this.address(),\n    ): User = User(id = id, name = name, address = address,)\n\n}\n```\n\nThe two generated methods, `create` and `invoke` do exactly the same. The latter is just for syntactic sugar (see example below).\n\nUsing this Faktroy, you can now simply create your own data factories\n\n```kotlin\nval validUser = UserSpec(\n    id = { UUID.randomUUID() },\n    name = { \"Peter\" }\n)\n```\n\nand use them in your test (or for generating example/test data)\n\n```kotlin\n@Test\nfun `it saves a user`() {\n    val user = validUser.create(name=\"Another name for my test\")\n    // alternatively, using `invoke`\n    // val user = validUser()\n\n    userRepository.save(user)\n\n    val allUsers = userRepository.findAll()\n    assertThat(allUsers).containsExactly(user)\n}\n```\n\nFor more examples, checkout the [`example-app`](/example-app).\n\n### Faktroy-bot for database interaction\n\nThe `create()` methods are great to create objects. However, when writing tests that interact with a database, it would be nice to have an easy way to insert data. For this, `faktory-bot` offers the `withInsert` option on the `@Faktory` annotation.\n\n```kotlin\n@Faktory(withInsert=true)\ndata class User(val id: UUID, val name: String, val address: Address?)\n```\n\nThis will add a parameter to the generated Faktroy: A function used for persistence.\n\n```kotlin\nclass UserRepositoryTest {\n\n    private val userRepository = UserRepository()\n    private val validUser = UserSpec(\n        faktoryInsertFn = userRepository::save,\n        id = { UUID.randomUUID() },\n        name = { \"Peter\" }\n    )\n\n    @Test\n    fun `it returns an inserted user`() {\n        val expectedUser = validUser.insert()\n\n        val allUsers = userRepository.findAll()\n\n        assertThat(allUsers).containsExactly(expectedUser)\n    }\n}\n```\n\nThis is just syntactic sugar to remove noise from your tests.\n\n### Best Practices\n\n`faktory-bot` is a [factory_bot](https://github.com/thoughtbot/factory_bot) clone for Kotlin. The API is modeled as closely as possible to factory bot.\n\nTherefore, best practices that apply to factory_bot also apply to `faktory-bot`.\n\n## Contribution\n\nWe welcome contributions to faktory-bot! If you would like to report a bug or request a feature,\nplease open an issue. If you want to submit a pull request, please make sure to include tests for\nyour changes.\n\n## Acknowledgments\n\nfaktory-bot is inspired by the following projects:\n\n- [factory_bot](https://github.com/thoughtbot/factory_bot)\n- [Fixtures](https://github.com/bluegroundltd/fixtures)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fraphiz%2Ffaktory-bot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fraphiz%2Ffaktory-bot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fraphiz%2Ffaktory-bot/lists"}