This utility library helps to generate testing data for POJOs using various settings.
To add library to your project perform next steps:
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>
Add the following dependency to your build.gradle:
implementation 'com.github.vladislavsevruk:test-data-generator:1.0.1'
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.
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());
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());
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);
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());
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);
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);
This project is licensed under the MIT License, you can read the full text here.