Thalia JUnit Rule for Testing with Postgres


License

License

Categories

Categories

JUnit Unit Testing
GroupId

GroupId

de.thalia.junit
ArtifactId

ArtifactId

postgres-rule
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Thalia JUnit Rule for Testing with Postgres
Thalia JUnit Rule for Testing with Postgres
Project URL

Project URL

https://github.com/ThaliaBuecherGmbH/thalia-junit-postgres
Project Organization

Project Organization

Thalia Bücher GmbH
Source Code Management

Source Code Management

https://github.com/ThaliaBuecherGmbH/thalia-junit-postgres

Download postgres-rule

How to add to project

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

Dependencies

compile (3)

Group / Artifact Type Version
ru.yandex.qatools.embed : postgresql-embedded jar 2.9
junit : junit jar 4.12
org.postgresql : postgresql jar 42.2.4

provided (1)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.18.2

Project Modules

There are no modules declared in this project.

Thalia JUnit Rule for Embedded PostgreSQL testing

Build Status Maven Central

Why

We often write complex and PostgreSQL specific JDBC code. This code is hard to test using mocks and embedded databases like Derby or H2. To get full test coverage we need a real database, we need a PostgreSQL!

This JUnit Rule makes it easy to spawn an embedded PostgreSQL instance for your JUnit tests, control it and shut it down when you are finished.

Minimum requirements

  • Java 1.8

How

Add the following Maven dependency to your Maven project:

<dependency>
    <groupId>de.thalia.junit</groupId>
    <artifactId>postgres-rule</artifactId>
    <version>1.0.0</version>
    <scope>test</scope>
</dependency>

Unit testing is quite easy, just take a look at the following code to see it in action:

import org.junit.Rule;
import org.junit.Test;

import de.thalia.junit.postgres.*;

public class PostgresEmbedderRuleTest {

    @Rule
    public PostgresEmbedderRule postgresEmbedderRule = PostgresEmbedderRule
        .builder()
        .withVersion(PostgresEmbedder.PostgreVersion.V10_3)
        .build();
    
    @Test
    public void testRule() throws SQLException {
        // Here we get the JDBC Datasource to the embedded instance
        final DataSource source = postgresEmbedderRule.getDataSource();
        
        // Do some JDBC stuff
        final ResultSet result = source
            .getConnection()
            .createStatement()
            .executeQuery("select current_date");
            
        while(result.next()) {
        }
        result.close();
        
        // We are done!
    }
}
de.thalia.junit

Thalia Bücher GmbH

Versions

Version
1.0.0