Faker: a library to generate fake data for Kotlin and Android
When writing tests for your application, it's common to make up data to fill your objects. You make up bogus names, emails, dates, addresses, prices... so that your validations still pass and you have realistic test cases. The same thing happens when you're trying to fill a QA database with fake information.
For ruby, I always use the faker gem. It has a big library of different fake data, already translated into more than 30 languages. In 2015 I decided to port it to Java, so I could use it in Android development.
Now, in 2023, I finally decided to migrate it to Kotlin. But don't worry, the interoperability with Java was maintained! And here is Faker 2.0:
The goals were:
- Reuse all the translated data
- Keep the interface simple and close to the original
How to install it:
repositories {
// ...
maven { url 'https://jitpack.io' }
}
dependencies {
// If you want to use in the APP itself besides testing
implementation 'com.github.blocoio:faker:2.0.1'
// ...
// Unit Testing
testImplementation 'com.github.blocoio:faker:2.0.1'
// Instrumentation Testing
androidTestImplementation 'com.github.blocoio:faker:2.0.1'
}
And some examples of what you can do with it Kotlin:
val faker = Faker()
faker.name.firstName() // "Aaron"
faker.company.name() // "Hirthe-Ritchie"
faker.address.countryCode() // "PT"
faker.commerce.price() // "31.89"
faker.internet.email() // "eliza@mann.net"
faker.lorem.sentence() // "Dolore illum animi et neque accusantium."
faker.number.number(10) // "1968353479"
faker.app.version() // "2.5.4"
val faker = Faker("nl")
faker.name.firstName() // "Thijs"
or java:
Faker faker = new Faker();
faker.name.firstName(); // "Aaron"
faker.company.name(); // "Hirthe-Ritchie"
faker.address.countryCode(); // "PT"
faker.commerce.price(); // "31.89"
faker.internet.email(); // "eliza@mann.net"
faker.lorem.sentence(); // "Dolore illum animi et neque accusantium."
faker.number.number(10); // "1968353479"
faker.app.version(); // "2.5.4"
Faker faker = new Faker("nl");
faker.name.firstName(); // "Thijs"
You can get all the information on the Read me.
This blog post was updated on 14/06/2023 with the intent of keeping this post information useful and relevant.