Databob provides a way to generate completely randomised object builders with zero-boilerplate code.

Programming language: Scala
License: Apache License 2.0
Tags: Testing     Data     Builder    

databob alternatives and similar packages

Based on the "Testing" category.
Alternatively, view databob alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of databob or a related project?

Add another 'Testing' Package



(Also available in JavaScript and Kotlin flavours)

Databob provides a way to generate completely randomised object builders with zero-boilerplate code.


The problem of generating dummy test instances for our classes has been around for a long time. Given the following case classes...

case class ReadReceipt(value: Boolean)

case class EmailAddress(value: String)

case class Email(from: EmailAddress, to: Seq[EmailAddress], date: ZonedDateTime, read: Boolean, subject: String, readReceipt: Try[ReadReceipt])

case class Inbox(address: EmailAddress, emails: Seq[Email])

We could start to write objects using the TestBuilder pattern using the traditional method:

class InboxAddressBuilder {
  private var address = EmailAddress("[email protected]")
  private var emails = Seq[Email]()

  def withAddress(newAddress: EmailAddress) = {
    address = newAddress

  def withEmails(newEmails: Seq[Email]) = {
    emails = newEmails

  def build = Inbox(address, emails)

Scala makes this easier for us somewhat by leveraging Case class copy(). This also allows us to be compiler safe, as removing a field will break the equivalent with method:

class InboxAddressBuilder {
  private var inbox = Inbox(EmailAddress("[email protected]"), Seq[Email]())

  def withAddress(newAddress: EmailAddress) = {
    inbox = inbox.copy(address = newAddress)

  def withEmails(newEmails: Seq[Email]) = {
    inbox = inbox.copy(emails = newEmails)

  def build = inbox

Taking this even further with default arguments, we can reduce this to:

object InboxAddressBuilder {
  def apply(address: EmailAddress = EmailAddress("[email protected]"),
            emails: Seq[Email] = Nil)
    = Inbox(address, emails)

So, better - but it still seems pretty tedious to maintain. Additionally, we don't really want tests to rely unknowingly on bits of default test data for multiple tests which will lead to an explosion of ObjectMother-type methods with small variations to suit particular tests.

What we really want are completely randomised instances, with important overrides set-up only for tests that rely on them. No sharing of test data across tests. Ever.

No sharing of test data across tests. Ever.

Enter Databob

For a completely randomised instance, including non-primitive sub-tree objects:


That's it. Want to override particular value(s)?

Databob.random[Inbox].copy(address = EmailAddress("[email protected]"))

Or add your own rule for generating values?

implicit val generators = typeIs(databob => EmailAddress(databob.mk[String] + "@" + databob.mk[String] + ".com")) +: Generators.EmptyGenerators


Additionally, once we have this randomising structure in code, we can use it for other useful stuff. For example, generating JSON with a library such as Json4S:

case class Book(title: String, pages: Int)

case class Teacher(firstName: String, lastName: String)

case class SchoolLibrary(librarian: Teacher, books: Seq[Book])

implicit val f = DefaultFormats

... would generate us something like this:


Or to get XML...


...producing this:


Out-of-the-box features:

  • Nested object-trees (ie. non-primitive fields)
  • All Scala/Java primitives: Default, random
  • Scala and Java Collection classes: Empty, single-value, variable size, random
  • Java8 date-time values: Epoch, current-time, random
  • Some monadic types (Option/Either/Try/Future): Happy, unhappy, random
  • Simple overriding mechanism for your own-types and custom generation rules

See it in action

See the example code.

Get it

Add the following lines to build.sbt:

resolvers += "JCenter" at "https://jcenter.bintray.com"
libraryDependencies += "io.github.daviddenton" %% "databob" % "1.5.0"


PRs gratefully accepted for other common types that might be useful.


To Json4S for the ninja reflection util.