org.hglteam.testing:jpatesting-postgres

Small set of classes to setup JPA on tests

Лицензия

Лицензия

Группа

Группа

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

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

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

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

1.0.2
Дата

Дата

Тип

Тип

jar
Описание

Описание

Small set of classes to setup JPA on tests

Скачать jpatesting-postgres

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

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

Зависимости

compile (3)

Идентификатор библиотеки Тип Версия
org.hglteam.testing : jpatesting-api jar 1.0.2
org.hglteam.testing : jpatesting-core jar 1.0.2
org.junit.jupiter : junit-jupiter-api jar

provided (2)

Идентификатор библиотеки Тип Версия
org.projectlombok : lombok jar 1.18.12
org.postgresql : postgresql jar 42.2.12

test (3)

Идентификатор библиотеки Тип Версия
org.hibernate : hibernate-core jar 5.4.17.Final
org.hibernate : hibernate-entitymanager jar 5.4.17.Final
io.zonky.test : embedded-postgres jar 1.2.7

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

Данный проект не имеет модулей.

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