Test Data Generator

This utility library helps to generate testing data for POJOs using various settings.

License

License

Categories

Categories

Data
GroupId

GroupId

com.github.vladislavsevruk
ArtifactId

ArtifactId

test-data-generator
Last Version

Last Version

1.0.2
Release Date

Release Date

Type

Type

pom.sha512
Description

Description

Test Data Generator
This utility library helps to generate testing data for POJOs using various settings.
Project URL

Project URL

https://github.com/VladislavSevruk/TestDataGenerator
Source Code Management

Source Code Management

https://github.com/VladislavSevruk/TestDataGenerator/tree/master

Download test-data-generator

Dependencies

compile (1)

Group / Artifact Type Version
com.github.vladislavsevruk : type-resolver jar 1.0.3

runtime (2)

Group / Artifact Type Version
org.apache.logging.log4j : log4j-api jar 2.13.0
org.apache.logging.log4j : log4j-core jar 2.13.0

Project Modules

There are no modules declared in this project.

Build Status Quality Gate Status Code Coverage Maven Central

Test Data Generator

This utility library helps to generate testing data for POJOs using various settings.

Table of contents

Getting started

To add library to your project perform next steps:

Maven

Add the following dependency to your pom.xml:

<dependency>
      <groupId>com.github.vladislavsevruk</groupId>
      <artifactId>test-data-generator</artifactId>
      <version>1.0.1</version>
</dependency>

Gradle

Add the following dependency to your build.gradle:

implementation 'com.github.vladislavsevruk:test-data-generator:1.0.1'

Usage

TestDataGenerator uses POJO's public constructors and setters to create POJO and set generated preudorandom values to it. Setters name can be in both classic and fluent styles, e.g. field with name field can match setters with setField or field names and field type should match parameter type of setter. You can add your custom generator if you want to set custom generator logic or override logic of existent one or you can add custom generation logic for specific field.

Non-parameterized classes

Let's assume that we have following POJO:

public class TestModel {
    private Set<String> field1;
    private Integer field2;

    // getters and setters
}

All you have to do is to use TestDataGenerator.generate(Class<T>) method:

// generating POJO with pseudorandom values
TestModel testModel = new TestDataGenerator().generate(TestModel.class);
// verifying field values
Assertions.assertNotEquals(0, acceptorModel.getField1().size());
Assertions.assertNotNull(acceptorModel.getField1().get(0));
Assertions.assertNotNull(acceptorModel.getField2());

Parameterized classes

Let's assume that we have following generic POJO:

public class GenericModel<T> {

    private Set<String> field1;
    private T field2;

    // getters and setters
}

All you have to do is to use TestDataGenerator.generate(TypeProvider<T>) method:

// generating POJO with pseudorandom values
GenericModel<String> acceptorModel = new TestDataGenerator()
        .generate(new TypeProvider<GenericModel<String>>() {});
// verifying field values
Assertions.assertNotEquals(0, acceptorModel.getField1().size());
Assertions.assertNotNull(acceptorModel.getField1().get(0));
Assertions.assertNotNull(acceptorModel.getField2());

Generation configuration

You can override some default generation configuration parameters:

TestDataGenerationConfig config = TestDataGenerationConfig.builder()
        // sets prefix for literal values
        .testDataPrefix("Prefix")
        // sets postfix for literal values
        .testDataPostfix("Postfix")
        // sets min items number that will be generated for collections, maps and arrays
        .minItemsForCollections(3)
        // sets max items number that will be generated for collections, maps and arrays
        .maxItemsForCollections(15).build();
// generate model values
TestModel testModel = new TestDataGenerator(config).generate(TestModel.class);

Customization

Adding custom test data generators

If you want to set custom generation logic for any type or override logic of existent one you can implement NonParameterizedTypeDataGenerator for non-parameterized type or ParameterizedTypeDataGenerator for parameterized type and add it to TestDataGeneratorStorage using one of add* methods:

TestDataGenerationContextManager.getContext().getTestDataGeneratorStorage()
        .add(new SomeCustomTestDataGenerator());

Adding custom field mappings

If you want to set custom generation logic for any field you can add it to CustomFieldMappingStorage using addMapping method:

Field field = TestModel.class.getDeclaredField("field2");
TestDataGenerationContextManager.getContext().getCustomFieldMappingStorage()
        .addMapping(field, config -> 15);

Adding post generation hooks

If generation logic of some field value require generated values from another fields or you want to trigger some action after fields values are generated you can implement PostGenerationHook and add it to PostGenerationHookStorage using one of add, addBefore or addAfter methods:

public class OrderModel {

    private String id;
    private String title;
    private Long timestamp;

    // getters and setters
}
PostGenerationHook<OrderModel> postGenerationHook 
        = model -> model.setId(model.getTitle() + "_" + model.getTimestamp());
TestDataGenerationContextManager.getContext().getPostGenerationHookStorage()
        .add(OrderModel.class, postGenerationHook);

License

This project is licensed under the MIT License, you can read the full text here.

Versions

Version
1.0.2
1.0.1
1.0.0