Generating Data Classes with random values for unit-testing using Konveyor

Martin Dina Putra
Bootcampers
Published in
2 min readMar 30, 2023

Unit testing is a software testing technique that involves testing individual units or components of a software application in isolation from the rest of the system. The goal of unit testing is to ensure that each unit of code behaves as intended and produces the expected output. It is the foundation of a stable software.

Unit testing is an essential part of modern software development, and developers are encouraged to write tests for every unit of code they create. By doing so, they can catch bugs early, reduce the time and cost of testing, and deliver high-quality software to their customers.

Problem

One of the most common annoying problem that I face when developing unit-tests are having to instantiate a mock object for a class that contains a large number of fields. Take a look at the data class below for example

Instantiating this class means that we have to provide for each of these fields whether they’re relevant/irrelevant to our test-case.

This takes way too much time & energy to do. Even worse, if we only need the field id for a particular test-case, you still need to provide values for the other fields otherwise the code won’t compile(Sucks!). It gets even worse when we encounter a nested objects.

Furthermore, using a hard-coded values when it doesn’t mean anything is not a good practice for unit-testing. Randomized values can help uncover errors that may not be obvious with fixed inputs. For example, using random inputs can help expose issues related to rounding errors or boundary conditions.

Fortunately, I found an amazing library that can help us to solve this issue.

Konveyor

Konveyor is a library for generating Data classes with random values. With Konveyor, we no longer need to instantiate our mock objects manually, we can simply do it by using the randomBuild() function.

The library will generate a PrimitiveDataClass with random values assigned to each of it’s fields

Wonderful! What’s even cooler is that it supports nested object and enum fields.

Print out result from NestedObject.kt formatted

To use Konveyor, add the dependencies to your project

Maven:

<dependency>
<groupId>com.github.vacxe</groupId>
<artifactId>konveyor</artifactId>
<version>latest version</version>
<type>pom</type>
</dependency>

or Gradle:

repositories {
jcenter()
}

dependencies {
compile 'com.github.vacxe:konveyor:$latest_version'
}

You can find the latest version in the github repository

There are several caveats that you need to keep in mind.

The first one is for some reason the author decided that Boolean values will always be false.

Second is that collection data types (List, Set, LinkedList, etc) will always be empty.

Lastly, fields with setter functions should be initialized manualy. The library can only help you to initialize fields inside the constructor.

These issues can be easily resolved by using the .copy() function and provide your own values if you do need them.

--

--