"Generate ORM Entity Instances Faster for Mock Data"
Mocka is a Spring-based library for automatically generate ORM Entity instances for Mock Data. It can create from not only single entities but also complex entity relationships. By mapping files to entity classes or fields, it provides flexible and customizable instance generation. It supports Hibernate and MyBatis as ORM frameworks, and automatically detects which one is in use to scan and generate the appropriate entity instances.
You can create entity instances without knowing their fields:
Generator<Member> generator = springGenerator.getGenerator(Member.class);
Member member = generator
.generateType(GenerateType.SELF)
.ormType(ORMTyp.HIBERNATE)
.get();Such randomly generated ORM entity instances can be used in various contexts, such as testing, data populating, and more.
This project consists of two modules:
- Mocka Core – generates field values when instantiating entities.
- Mocka ORM – handles the logic for generating ORM entity instances.
Please visit the GitHub Wiki for detailed usage of each module.
| module | version | Github Page |
|---|---|---|
| mocka core | Core Github Page | |
| mocka orm | ORM Github Page |
- ORM Entity Instance Auto Generation : Easily instantiate ORM-managed entities without manually assigning fields.
- Multi-ORM Support : Automatically detects ORM frameworks (Hibernate, MyBatis) and scans entity classes to generate appropriate instances.
- Random Data Generation : Supports basic types such as String, Number, and Date, as well as Email, Lorem Ipsum, Name, IP Address, and more.
- Regex Pattern Generation : Supports generating strings that match the given regular expression patterns.
- Entity Relationship Support : Not only generates entities but also creates associated entities recursively.
- Extensibility : Implement your own custom generators to support new data types seamlessly.
- Flexibility : Map entity fields or entire classes to specific datasets or files for tailored instance generation.
- Test Optimization : Automatically generate large volumes of entities to accelerate unit, integration, and data-populating tests.
dependencies {
implementation 'io.github.yyytir777:mocka-core:1.3.0'
implementation 'io.github.yyytir777:mocka-orm:1.3.0'
}<dependencies>
<dependency>
<groupId>io.github.yyytir777</groupId>
<artifactId>mocka-core</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>io.github.yyytir777</groupId>
<artifactId>mocka-orm</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>Generator<Member> generator = springGenerator.getGenerator(Member.class);
Member member = generator
.generateType(GenerateType.SELF)
.ormType(ORMType.HIBERNATE)
.get();@Entity
public class Member {
@ValueSource(generatorKey = "name")
private String name;
}@Entity
public class Member {
@ValueSource(path = "name.txt", type=String.class)
private String name;
}- Supports YAML, XML, JSON, and CSV
@Entity
@FileSource(path = "member.csv")
public class Member {
}@Entity
public class Member {
@RegexSource(pattern = "[a-zA-Z]{10}")
private String name;
}GeneratorFactory.putGenerator(new RegistrableGenerator<>("test_generator", "test.txt", String.class));
Generator<String> generator = GeneratorFactory.getGenerator("test_generator");- You can easily initialize entity instances in test code.
- Developers can focus on core logic without having to manually generate relational entity instances for each test method
@SpringBootTest
@ExtendWith(MockaExtension.class)
public class MockaTest {
@Mocka
Parent parent;
@Test
void assert_parent_is_not_null() {
assertThat(parent).isNotNull();
}
}@MockaConfigannotation defines configuration options for entity instance generation in tests.size: Specifies how many entity instances should be generated for1:nassociations. (Default : 5)ormType: Specifies which ORM implementation (HIBERNATE,MYBATIS) to use (when multiple are available)
@MockaConfig(size = 10, ormType = ORMType.HIBERNATE)
public class MockaTest {
@Mocka(GenerateType.CHILD)
Parent parent;
@Test
void generate_children_with_given_size_and_specified_orm() {
assertThat(parent.getChildren()).hasSize(10);
}
}When generating entity instances, you can specify the GenerateType to control how associated entities are created.
In the diagram above, entity A and B have a 1:N relationship, and B and C also have a 1:N relationship. Let’s use this to explain each GenerateType strategy.
Generates only the specified entity instance.
Even if relationships exist, they are initialized as null.
Generator<B> generator = springGenerator.getGenerator(B.class);
B b = generator
.generateType(GenerateType.SELF)
.get();If you generate entity B with GenerateType.SELF, only B will be created; associated entities will not be generated.
Generates the specified entity and its direct child entities.
Generator<B> generator = springGenerator.getGenerator(B.class);
B b = generator
.generateType(GenerateType.CHILD)
.get();If you generate entity B with GenerateType.CHILD, both B and its child entity C will be created.
Recursively generates the specified entity and all its descendant entities.
Generator<B> generator = springGenerator.getGenerator(B.class);
B b = generator
.generateType(GenerateType.CHILDREN)
.get();If you generate entity A with GenerateType.CHILDREN, entities A, B, and C will all be created recursively.
Generates the specified entity and its direct parent entity.
Generator<B> generator = springGenerator.getGenerator(B.class);
B b = generator
.generateType(GenerateType.PARENT)
.get();If you generate entity B with GenerateType.PARENT, both B and its parent entity A will be created.
Recursively generates the specified entity and all its ancestor entities.
Generator<B> generator = springGenerator.getGenerator(B.class);
B b = generator
.generateType(GenerateType.PARENTS)
.get();If you generate entity C with GenerateType.PARENTS, entities C, B, and A will all be created recursively.
Recursively generates the specified entity and all related entities, both parents and children.
Generator<B> generator = springGenerator.getGenerator(B.class);
B b = generator
.generateType(GenerateType.ALL)
.get();If you generate entity B with GenerateType.ALL, entities A, B, and C will all be created together.
If you want to see more information of Mocka, please visit GitHub Wiki Pages!!







