org.hglteam.testing:jpatesting-providers

Small set of classes to setup JPA on tests

License

License

Categories

Categories

IDE Development Tools
GroupId

GroupId

org.hglteam.testing
ArtifactId

ArtifactId

jpatesting-providers
Last Version

Last Version

1.0.2
Release Date

Release Date

Type

Type

pom
Description

Description

Small set of classes to setup JPA on tests

Download jpatesting-providers

Filename Size
jpatesting-providers-1.0.2.pom 640 bytes
Browse

How to add to project

<!-- 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"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

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

Versions

Version
1.0.2