Skip to content

Are there any tools to create dummy objects to use for JUnit test cases?

I am writing JUnit test cases to test CRUD operations in DAO classes. It is pretty much boilerplate code and bulk of it is to create the test object and assign dummy values to the instance variables. Are there any tools in Java to create an object and assign dummy values based on the declared type?

I don’t want to use JMock or Mockito as I need to interact with the database and test that the CRUD operations are successful.


I don’t know a tool which will create a mock object for you and fill it automatically with some fuzzy data.

But maybe the following approach would be close to what you want to achieve.

pseudo code

Foo foo = new Foo();
// store the record in the database
// retrieve the record from the database
// check if the retrieved values are equal to the values in `foo`

If this is what you want to achieve you might have a look for QuickCheck

The goal of QuickCheck is to replace manually picked values with generated values. A QuickCheck-based test tries to cover the laws of a domain whereas classical testing can only test the validity for distinct values.

Basically, QuickCheck is about generators of data. The QuickCheck runner method is just a fancy for loop implementation. QuickCheck can help in scenarios where whole classes of test cases have to be tested and it is not feasible to write tests for all distinct test scenarios.

So you could create your own Generator which provides instances of Foo already filled with fuzzy data. In combination with Junit Theories you would be close to your initial requirement.

An example for using Theories with a custom generator you can find here. The example was written for a similar library (junit-quickcheck). But it should demonstrate your the idea.

edit Roughly based on the junit-quickcheck example. It might look as in the following snippet.

import static;

public class MyCharacterGenerator implements Generator<String> {

    private static final String LOWERCASE_CHARS = "abcdefghijklmnopqrstuvwxyz";
    private static final String NUMBERS = "0123456789";
    private static final String SPECIAL_CHARS = ".-\;:[email protected][]^/|}{";
    private static final String ALL_MY_CHARS = LOWERCASE_CHARS
    public static final int CAPACITY = 40;

    Generator<Character> characterGenerator = characters(ALL_MY_CHARS);

    public String generate() {
        StringBuilder sb = new StringBuilder(CAPACITY);
        for (int i = 0; i < CAPACITY; i++) {
        return sb.toString();

    public String next() {
        return generate();


import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;

public class MyCharacterGeneratorTest {

    public void testStringGenerator() {
        Generator<String> fuzzyString = new MyCharacterGenerator();

        for (int i = 0; i < 10; i++) {
            String fuzzy =;
            System.out.println("fuzzy: " + fuzzy);
            assertTrue(fuzzy.length() == MyCharacterGenerator.CAPACITY);
            assertTrue(fuzzy.matches("[a-zA-Z0-9.\-\\;:[email protected]\[\]^/|}{]*"));


 T E S T S
Running MyCharacterGeneratorTest
fuzzy: ;d|xrS|[email protected]{[email protected];de5:jN
fuzzy: UCZBO|QJ/[email protected];b
fuzzy: Jg}xD44_AFVqyUKMehGPnV8xmKKy]dDXJsYIG9C
fuzzy: k-eN-Sf^eK.bqqn4PR2[93{wyzgwr_F_ktBGkTP}
fuzzy: 1UDChf3aWN0d/[email protected]}K[W2|P]}.ePzKvRJMJtB0/Z
fuzzy: @J2}[email protected]]smpKWZ;[email protected]}[email protected]
fuzzy: goL8o5qz-Ynga:i;WqGhKTo^1itHqENXM3OrO||4
fuzzy: _1ifR:ssplcdT9ls{clV9ZozgCA^I67IF/|t0t
fuzzy: /FwL9nCuRcemqR2SP3|XG9ui5Y21K:r0Ys1XIz/3
fuzzy: 8U[Xk^e60JhGfLTMyGZ:Z;[email protected]]