org.hglteam.testing:jpatesting-api

Small set of classes to setup JPA on tests

License

License

GroupId

GroupId

org.hglteam.testing
ArtifactId

ArtifactId

jpatesting-api
Last Version

Last Version

1.0.2
Release Date

Release Date

Type

Type

jar
Description

Description

Small set of classes to setup JPA on tests

Download jpatesting-api

How to add to project

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

Dependencies

provided (3)

Group / Artifact Type Version
jakarta.persistence : jakarta.persistence-api jar 2.2.3
org.hibernate : hibernate-core jar 5.4.17.Final
org.projectlombok : lombok jar 1.18.12

Project Modules

There are no modules declared in this project.

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");

Versions

Version
1.0.2