org.hglteam.testing:jpatesting-providers

Small set of classes to setup JPA on tests

Лицензия

Лицензия

Категории

Категории

IDE Инструменты разработки
Группа

Группа

org.hglteam.testing
Идентификатор

Идентификатор

jpatesting-providers
Последняя версия

Последняя версия

1.0.2
Дата

Дата

Тип

Тип

pom
Описание

Описание

Small set of classes to setup JPA on tests

Скачать jpatesting-providers

Имя Файла Размер
jpatesting-providers-1.0.2.pom 640 bytes
Обзор

Как подключить последнюю версию

<!-- https://jarcasting.com/artifacts/org.hglteam.testing/jpatesting-providers/ -->
<dependency>
    <groupId>org.hglteam.testing</groupId>
    <artifactId>jpatesting-providers</artifactId>
    <version>1.0.2</version>
    <type>pom</type>
</dependency>
// https://jarcasting.com/artifacts/org.hglteam.testing/jpatesting-providers/
implementation 'org.hglteam.testing:jpatesting-providers:1.0.2'
// https://jarcasting.com/artifacts/org.hglteam.testing/jpatesting-providers/
implementation ("org.hglteam.testing:jpatesting-providers:1.0.2")
'org.hglteam.testing:jpatesting-providers:pom:1.0.2'
<dependency org="org.hglteam.testing" name="jpatesting-providers" rev="1.0.2">
  <artifact name="jpatesting-providers" type="pom" />
</dependency>
@Grapes(
@Grab(group='org.hglteam.testing', module='jpatesting-providers', version='1.0.2')
)
libraryDependencies += "org.hglteam.testing" % "jpatesting-providers" % "1.0.2"
[org.hglteam.testing/jpatesting-providers "1.0.2"]

Зависимости

Библиотека не имеет зависимостей. Это самодостаточное приложение, которое не зависит ни от каких других библиотек.

Модули Проекта

  • jpatesting-h2
  • jpatesting-postgres

jpatesting

It's a simple set of classes to setup JPA on tests.

We provide two implementations out of the box, H2 and postgres databases. But is pretty straight-forward to use another vendor datasource. If you'd like to contribute to the project feel free to contact us.

Example using Postgres provider

You can connect to Postgresql database using embedded or external datasource.

Embedded datasource

In this example we use Zonky's embedded postgres to setup an embedded postgres instance.

<dependency>
    <groupId>org.hglteam.testing</groupId>
    <artifactId>jpatesting-postgres</artifactId>
    <version>1.0.2</version>
</dependency>

<!-- Embedded postgres instance and driver -->
<dependency>
    <groupId>io.zonky.test</groupId>
    <artifactId>embedded-postgres</artifactId>
    <version>1.2.7</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.2.12</version>
</dependency>

<!-- You must provide yout JPA implementation in test scope -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <scope>test</scope>
</dependency>
class PostgresJpaConfigurerTest {
    ...
    
    @RegisterExtension
    public static PreparedDbExtension extension = EmbeddedPostgresExtension.preparedDatabase(e -> { });

}

finaly just setup your entity manager factory via the configurer.

@BeforeAll
static void emptyEntityManagerSuccess() {
    var configurer = PostgresJpaConfigurer.begin(extension::getTestDatabase)    // pass a datasource provider to the initializer
            .properties()
                .schemaGenerationDatabaseAction(JpaPropertyConfigurer.DatabaseAction.DROP_AND_CREATE)
                .and()
            .persistenceUnitName("emptyEntityManagerSuccess")
            .withEntity(ManagedTestEntity.class);

    emf = configurer.buildFactory();    // creates a EntityManagerFactory
    em = emf.createEntityManager();     // gets your entity manager
}

Configuring the persistence unit

You can configure the persistence unit by adding class by class

configurer = PostgresJpaConfigurer.begin(extension::getTestDatabase)
        .properties()
            .schemaGenerationDatabaseAction(JpaPropertyConfigurer.DatabaseAction.DROP_AND_CREATE)
            .and()
        .persistenceUnitName("emptyEntityManagerSuccess")
        .withEntity(TestEntity1.class)
        .withEntity(TestEntity2.class)
        ...
        ;

or by adding an xml mapping file

configurer = PostgresJpaConfigurer.begin(extension::getTestDatabase)
        .properties()
            .schemaGenerationDatabaseAction(JpaPropertyConfigurer.DatabaseAction.DROP_AND_CREATE)
            .and()
        .persistenceUnitName("emptyEntityManagerSuccess")
        .withMapping("/META-INF/mappings/entity-mapping-1.xml")
        .withMapping("/META-INF/mappings/entity-mapping-2.xml")
        ...
        ;

you can also use a persitence.xml file but atomic class approach is better for tests.

external datasource

If you have to connect to an external datasource, you must provide the jdbc connection and credentials.

configurer = PostgresJpaConfigurer.begin()  // No datasource provider is required within the initializer.
        .properties()
            .schemaGenerationDatabaseAction(JpaPropertyConfigurer.DatabaseAction.DROP_AND_CREATE)
            .url("jdbc:postgresql://localhost:5432/my_external_db")
            .username("my_username")
            .password("my_password")
        .and()
        .persistenceUnitName("emptyEntityManagerSuccess")
        .withMapping("/META-INF/mappings/mapping.xml");

Версии библиотеки

Версия
1.0.2