Skip to content

yyytir777/Mocka

Repository files navigation

Mocka

Test Entity_Instantiator Library Quality Gate Status Bugs Coverage

"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 Maven Central Version Core Github Page
mocka orm Maven Central Version ORM Github Page

Key Features

  • 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.

Import Library

Gradle

dependencies {
    implementation 'io.github.yyytir777:mocka-core:1.3.0'
    implementation 'io.github.yyytir777:mocka-orm:1.3.0'
}

Maven

<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>

What Can You Do With It?

1. Generate Entity Instances

Generator<Member> generator = springGenerator.getGenerator(Member.class);
Member member = generator
        .generateType(GenerateType.SELF)
        .ormType(ORMType.HIBERNATE)
        .get();

2. Map Entity Fields to a Generator or File

@Entity
public class Member {
    @ValueSource(generatorKey = "name")
    private String name;
}
@Entity
public class Member {
    @ValueSource(path = "name.txt", type=String.class)
    private String name;
}

3. Map Entire Entities to Files

  • Supports YAML, XML, JSON, and CSV
@Entity
@FileSource(path = "member.csv")
public class Member {
    
}

4. Map Entity Fields to Regex Patterns

@Entity
public class Member {
    @RegexSource(pattern = "[a-zA-Z]{10}")
    private String name;
}

5. Register Custom Generator based specific file to GeneratorFactory

GeneratorFactory.putGenerator(new RegistrableGenerator<>("test_generator", "test.txt", String.class));
Generator<String> generator = GeneratorFactory.getGenerator("test_generator");

@Mocka Annotation

  • 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();
    }
}

@MockaConfig Annotation

  • @MockaConfig annotation defines configuration options for entity instance generation in tests.
  • size : Specifies how many entity instances should be generated for 1:n associations. (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);
    }
}

GenerateType

When generating entity instances, you can specify the GenerateType to control how associated entities are created.

GenerateType.png

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.


GenerateType.SELF

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.

GenerateType SELF.png


GenerateType.CHILD

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.

GenerateType CHILD.png


GenerateType.CHILDREN

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.

GenerateType CHILDREN.png


GenerateType.PARENT

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.

GenerateType PARENT.png


GenerateType.PARENTS

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.

GenerateType PARENTS.png


GenerateType.ALL

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.

GenerateType ALL.png


GitHub Wiki

If you want to see more information of Mocka, please visit GitHub Wiki Pages!!

About

Generate ORM Entity Instances Faster for Mock Data

Topics

Resources

License

Stars

Watchers

Forks

Contributors 2

  •  
  •