scalacheck-faker
Fake Data Generation using ScalaCheck
This library ports over common Faker routines from popular implementations, such as Java Faker into a Scala library.
Installation
libraryDependencies += "io.github.etspaceman" %% "scalacheck-faker" % "8.0.5"
Arbitraries
Data generators are provided in the form of ScalaCheck Arbitraries. In the below example, we use the instances to construct an Arbitrary instance for our own class.
import org.scalacheck._
import faker._
// Import the default resource-loader implicits
import faker.ResourceLoader.Implicits._
case class MyClass(firstName: String, lastName: String, emailAddress: String, ipAddress: String)
object MyClass {
implicit val myClassArbitrary: Arbitrary[MyClass] = Arbitrary(
for {
firstName <- Arbitrary.arbitrary[name.FirstName]
lastName <- Arbitrary.arbitrary[name.LastName]
email <- Arbitrary.arbitrary[internet.EmailAddress]
ip <- Arbitrary.arbitrary[internet.IpV4Address]
} yield MyClass(firstName.value, lastName.value, email.value, ip.value)
)
}
The above uses import faker.ResourceLoader.Implicits._
, which provides the default ResourceLoader for the
Arbitrary instances. This ResourceLoader will evaluate the Faker instances against your system's default
Locale value. You can override this value in your application's JVM
through JVM args.
If you have a need to use a separate locale than the system default, you can create your own instance:
import faker.ResourceLoader
object UKResourceLoader {
implicit val UKResourceLoader: ResourceLoader = ResourceLoader.en_GB
}
Faker Class
Another option for generating data is the Faker class. This class provides a similar feel to other Faker libraries.
import java.time.Instant
import org.scalacheck.rng.Seed
import faker._
val firstName: String = Faker.default.firstName()
val nowInstant: Instant = Faker.default.nowInstant()
// You can also leverage a Faker instance using a manually-designated Locale:
val ukFirstName: String = Faker.en_GB.firstName()
// Sometimes you need to be able to manually supply the seed for the random number generator.
// You can do that below:
val firstNameSeeded: String = Faker.default.firstName(Seed(2L))
Un-implemented instances
Some locales may cause Faker to return dummy values for certain instances. For example,
there are no StateLike
instances for the UK (en_GB) Locale.
Some instances may not be implemented due to the support not being present in some other Faker library. If you see some instances that should exist for a locale, and have values that would be relevant for it, please either open an Issue or PR with the proposed implementation.