Dropwizard hibernate test util

Hibernate utility class for dropwizard applications

License

License

Categories

Categories

DropWizard Container Microservices Hibernate Data ORM
GroupId

GroupId

com.github.mtakaki
ArtifactId

ArtifactId

dropwizard-hibernate-test-util
Last Version

Last Version

1.3.8
Release Date

Release Date

Type

Type

jar
Description

Description

Dropwizard hibernate test util
Hibernate utility class for dropwizard applications
Project URL

Project URL

https://github.com/mtakaki/dropwizard-hibernate-test-util
Source Code Management

Source Code Management

https://github.com/mtakaki/dropwizard-hibernate-test-util

Download dropwizard-hibernate-test-util

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.mtakaki/dropwizard-hibernate-test-util/ -->
<dependency>
    <groupId>com.github.mtakaki</groupId>
    <artifactId>dropwizard-hibernate-test-util</artifactId>
    <version>1.3.8</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.mtakaki/dropwizard-hibernate-test-util/
implementation 'com.github.mtakaki:dropwizard-hibernate-test-util:1.3.8'
// https://jarcasting.com/artifacts/com.github.mtakaki/dropwizard-hibernate-test-util/
implementation ("com.github.mtakaki:dropwizard-hibernate-test-util:1.3.8")
'com.github.mtakaki:dropwizard-hibernate-test-util:jar:1.3.8'
<dependency org="com.github.mtakaki" name="dropwizard-hibernate-test-util" rev="1.3.8">
  <artifact name="dropwizard-hibernate-test-util" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.mtakaki', module='dropwizard-hibernate-test-util', version='1.3.8')
)
libraryDependencies += "com.github.mtakaki" % "dropwizard-hibernate-test-util" % "1.3.8"
[com.github.mtakaki/dropwizard-hibernate-test-util "1.3.8"]

Dependencies

compile (2)

Group / Artifact Type Version
io.dropwizard : dropwizard-hibernate jar 1.3.8
org.hsqldb : hsqldb jar 2.4.1

provided (2)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.18.4
junit : junit jar 4.12

test (2)

Group / Artifact Type Version
org.mockito : mockito-all jar 1.10.19
org.assertj : assertj-guava jar 3.2.1

Project Modules

There are no modules declared in this project.

Status

CircleCI Coverage Status Codacy Badge Download Javadoc License

dropwizard-hibernate-test-util

Hibernate utility class for writing integration tests for AbstractDAO classes in dropwizard applications. It uses an in-memory database (HSQLDB) and uses hibernate functionality to auto-create the tables on startup.

This library follows what is described in this blog post: http://www.petrikainulainen.net/programming/testing/writing-tests-for-data-access-code-unit-tests-are-waste/ Bugs often slips through the unit tests with mocks. Using an in-memory database allows you to actually run the SQL scripts and remove the effort of writing mocks.

Supported versions:

Dropwizard Hibernate test util
0.9.3 0.0.1
0.9.3 0.0.2
1.3.8 1.3.8

Maven

The library is available at the maven central, so just add the dependency to pom.xml with scope set to test:

<dependencies>
    <dependency>
        <groupId>com.github.mtakaki</groupId>
        <artifactId>dropwizard-hibernate-test-util</artifactId>
        <version>1.3.8</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Example

The class HibernateDAOTestUtil provides a jUnit Rule, so it can be simply used like this:

public class TestEntityDAOTest {
    @Rule
    public HibernateDAOTestUtil testUtil = new HibernateDAOTestUtil(TestEntity.class);
    
    private TestEntityDAO dao;

    public void setup() {
        this.dao = new TestEntityDAO(this.testUtil.getSessionFactory());
    }

    @Test
    public void testSaveAndQuery() {
        // We have a session opened and ready to be used.
        final Session session = this.testUtil.getSession();
        final TestEntity entity = TestEntity.builder().body("testing writing").build();
        session.save(entity);

        final Optional<TestEntity> foundEntityOptional = this.dao.findById(entity.getId());
        assertThat(foundEntityOptional).isPresent().contains(entity);
    }
}

The schema is completely dropped after each test, guaranteeing test isolation. So no need to clean up after yourself, just let the database be destroyed after the test.

Versions

Version
1.3.8
0.0.2
0.0.1