All Projects → triologygmbh → test-data-loader

triologygmbh / test-data-loader

Licence: MIT License
A Groovy DSL for creating test data via JPA

Programming Languages

groovy
2714 projects

Projects that are alternatives of or similar to test-data-loader

random-jpa
Create random test data for JPA/Hibernate entities.
Stars: ✭ 23 (+91.67%)
Mutual labels:  jpa, entity, test-data, test-data-generator
h2go
Apache H2 Go SQL Driver
Stars: ✭ 35 (+191.67%)
Mutual labels:  h2, h2-database
Generatedata
A powerful, feature-rich, random test data generator.
Stars: ✭ 1,883 (+15591.67%)
Mutual labels:  test-data, test-data-generator
Hunt Entity
An object-relational mapping (ORM) framework for D language (Similar to JPA / Doctrine), support PostgreSQL and MySQL.
Stars: ✭ 51 (+325%)
Mutual labels:  jpa, entity
migrate-Java-EE-app-to-azure
Migrate an existing Java EE workload to Azure
Stars: ✭ 12 (+0%)
Mutual labels:  jpa, wildfly
vogon-java
Vogon - A simple personal finance tracker using Spring Boot and AngularJS
Stars: ✭ 16 (+33.33%)
Mutual labels:  jpa, h2
Faker
Faker is a Python package that generates fake data for you.
Stars: ✭ 13,401 (+111575%)
Mutual labels:  test-data, test-data-generator
spring-boot-elk
An sample todo app demonstrating centralised logging using ELK stack
Stars: ✭ 20 (+66.67%)
Mutual labels:  jpa, h2-database
Spring-Boot-2
Spring Boot 2.x examples
Stars: ✭ 33 (+175%)
Mutual labels:  jpa, h2
o365
O365管理系统是一个以java语言开发的基于Microsoft Graph Restful API的多全局管理系统,理论上支持任何Office全局的管理(A1,A3,A1P,E3,E5等),你可以很方便的使用它来批量添加,批量删除,批量启用,批量禁用,搜索和查看用户,绑定解绑域名,生成邀请码,邀请朋友注册,提升和收回管理员权限,更新密钥,查看订阅,分配订阅(创新用户时),查看多全局报告
Stars: ✭ 281 (+2241.67%)
Mutual labels:  jpa, h2-database
microprofile1.2-samples
Eclipse MicroProfile 1.2 Samples
Stars: ✭ 22 (+83.33%)
Mutual labels:  wildfly, wildfly-swarm
SimpleCurd
2个类,实现类ActiveRecord,无需写Mapper, mybatis增强
Stars: ✭ 14 (+16.67%)
Mutual labels:  jpa, entity
nextras-orm-generator
🐺 Rich Nextras\ORM entity generator for fast prototyping your new project.
Stars: ✭ 19 (+58.33%)
Mutual labels:  entity
springboot-chapter
🚀Spring Boot 2.0基础教程。主流框架整合,实践学习案例。
Stars: ✭ 23 (+91.67%)
Mutual labels:  jpa
betca-spring
BETCA (Spring). Back-end con Tecnologías de Código Abierto, versión Java-Spring
Stars: ✭ 27 (+125%)
Mutual labels:  jpa
NPC
The NPC plugin for PocketMine-MP
Stars: ✭ 28 (+133.33%)
Mutual labels:  entity
URSA
[DEPRECATED] integrated ECS framework for Unity
Stars: ✭ 30 (+150%)
Mutual labels:  entity
LazyREST
基于Sping全家桶实现的极速产出RESTful类接口脚手架
Stars: ✭ 21 (+75%)
Mutual labels:  jpa
spring-boot-examples
本仓库为《Spring Boot 系列文章》代码仓库,欢迎点赞、收藏。
Stars: ✭ 52 (+333.33%)
Mutual labels:  jpa
lovelace-battery-entity-row
Show battery states or attributes with dynamic icon on entity rows in Home Assistant's Lovelace UI
Stars: ✭ 49 (+308.33%)
Mutual labels:  entity

test-data-loader

Build Status Maven Central

Overview

This project implements a Groovy DSL that can be used to populate a database using JPA entities. Its indented use is testing but it could be used in other scenarios as well. The DSL is implemented in Groovy but can be used from pure Java. Entities are modularly defined in separate .groovy files using the DSL syntax. Those entity definition files can then be loaded as needed using the de.triology.blog.testdata.loader.TestDataLoader, which also provides access to loaded entities. Thus, the client code does not need to deal with any database or JPA specific concerns other than providing an initialized EntityManager.

This project was started while working on an article published in Java aktuell 03/2017: A Groovy DSL for the Creation of Test Data using JPA.
The original article (🇩🇪) can be found here: Eine Groovy-DSL zum Erzeugen von Testdaten über JPA.

Please note that from version 1.x the implementation as described in the article referenced above has changed. For more information about the changes, please refer to the release notes of each release.

Configuration

Add the latest stable version of test-data-loader to the dependency management tool of your choice.

E.g. for maven

<dependency>
    <groupId>de.triology.test-data-loader</groupId>
    <artifactId>test-data-loader</artifactId>
    <version>1.0.0</version>
</dependency>

Current version is Maven Central

You can get snapshot versions from maven central (for the most recent commit on develop branch) or via JitPack (note that JitPack uses different maven coordinates).

Usage

An example entity definition Groovy script file can be found here: https://github.com/triologygmbh/test-data-loader/blob/master/src/test/resources/tests/itTestData.groovy

The de.triology.blog.testdata.loader.TestDataLoaderIT integration test shows how to load that file.

Entity Definitions

Use the following syntax in a separate .groovy file to define a User entity. The entity will be created, persisted and registered under the name "Peter" when the definition file is loaded. Note: Entity definition files are expected to be UTF-8 encoded.

import de.triology.blog.testdata.loader.testentities.User

create User, 'Peter', {
    id = 123
    firstName = 'Peter'
    lastName = 'Pan'
    login = 'pete'
    email = '[email protected]'
}

Create nested entities by simply nesting their definitions:

import de.triology.blog.testdata.loader.testentities.User
import de.triology.blog.testdata.loader.testentities.Department

create User, 'Peter', {
    // ...
    department = create Department, 'lostBoys', {
        id = 999
        name = 'The Lost Boys'
    }
}

And reference previously created entities by their name like so:

import de.triology.blog.testdata.loader.testentities.User
import de.triology.blog.testdata.loader.testentities.Department

create User, 'Peter', {
    // ...
    department = create Department, 'lostBoys', {
        // ...
        head = Peter
    }
}

create User, 'Tinker', {
    id = 555
    firstName = 'Tinker'
    lastName = 'Bell'
    department = lostBoys
}

Since entity definition files are just plain Groovy scripts, you are free to use any control structures, like loops and conditions, e.g.:

import de.triology.blog.testdata.loader.testentities.User

5.times { count ->
    create User, "user_$count", {
        id = 1000 + count
        if(count % 2 == 0) {
            firstName = "even_$count"
        } else {
            firstName = "odd_$count"
        }
    }
}

Private fields from superclasses

For now, we must work around this, by setting the field to protected or creating a protected setter method. See this issue.

Loading entity definitions

Use the de.triology.blog.testdata.loader.TestDataLoader to load entity definition files (from classpath or file system) and persist the defined entities. The TestDataLoader requires a fully initialized, ready-to-use EntityManager and can then be used to load entity definition files and access the persisted entities.

EntityManager entityManager = // ... init EntityManager
TestDataLoader testDataLoader = new TestDataLoader(entityManager);
testDataLoader.loadTestData(Collections.singletonList("demo/testData.groovy"));

User peter = entityManager.find(User.class, 123L);
assert "Pan".equals(peter.getLastName());

User tinker = testDataLoader.getEntityByName("Tinker", User.class);
assert "Bell".equals(tinker.getLastName());

Clean up afterwards

To reset the database as well as the TestDataLoader to a clean state after a test case simply call testDataLoader.clear(). That will delete all created entities from the database and from TestDataLoader's entity cache.

Tested with...

We have approved TestDataLoader in multiple projects and use cases including

  • "unit" tests with H2 and JUnit
  • Integration test with arquillian, WildFly (Swarm) and Postgresql
  • Integration tests with arquillian, IBM WebSphere Liberty Profile and IBM DB2

Contributions

The test-data-loader has been derived and generalized from real world development projects but has yet to prove itself as stand-alone library. Any feedback or contributions are highly welcome!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].