Skip to content

JUnit Rule for providing a H2 Embedded in-memory database for your tests

License

Notifications You must be signed in to change notification settings

ahaarrestad/embedded-db-junit

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

75 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

embedded-db-junit

Build Status Coverage Status Maven Central Analytics Apache V2 License

JUnit Rule that provides a H2 Embedded in-memory database. It is compatible with all known JDBC access libraries such as Spring JDBC, RX-JDBC, sql2o, JDBI or plain old JDBC.

Why?

  • because you want to test the SQL code executed by your code without integrating with an actual DB server
  • removes the need of having a database server running and available
  • you are refactoring legacy code where JDBC calls is tightly coupled with your business logic and wants to start by testing the legacy code from the "outside" (as suggested by Michael Feathers)

Status

This library is distributed through the Sonatype OSS repo and should thus be widely available. Java 7 or higher is required.

Changelog

  • version 0.3: updated all dependencies as well as some changes to the internal implementation
  • version 0.2: added datasource() for getting an embedded DataSource and suppressing close() call to the connection
  • version 0.1: first release

Usage

Add dependency

Maven

<dependency>
    <groupId>org.zapodot</groupId>
    <artifactId>embedded-db-junit</artifactId>
    <version>0.3</version>
</dependency>

SBT

libraryDependencies += "org.zapodot" % "embedded-db-junit" % "0.3"

Add to Junit test

@Rule
public EmbeddedDatabaseRule dbRule = EmbeddedDatabaseRule
                                        .builder()
                                        .withMode("ORACLE")
                                        .withInitialSql("CREATE TABLE Customer(id INTEGER PRIMARY KEY, name VARCHAR(512)); "
                                                        + "INSERT INTO CUSTOMER(id, name) VALUES (1, 'John Doe')")
                                        .build();

@Test
public void testUsingRxJdbc() throws Exception {
    assertNotNull(dbRule.getConnection());
    final Database database = Database.from(dbRule.getConnection());
    assertNotNull(database.select("SELECT sysdate from DUAL")
                  .getAs(Date.class)
                  .toBlocking()
                  .single());

    assertEquals("John Doe", database.select("select name from customer where id=1")
                                     .getAs(String.class)
                                     .toBlocking()
                                     .single());
}

@Test
public void testUsingSpringJdbc() throws Exception {

    final JdbcOperations jdbcOperation = new JdbcTemplate(dbRule.getDataSource());
    final int id = 2;
    final String customerName = "Jane Doe";

    final int updatedRows = jdbcOperation.update("INSERT INTO CUSTOMER(id, name) VALUES(?,?)", id, customerName);

    assertEquals(1, updatedRows);
    assertEquals(customerName, jdbcOperation.queryForObject("SELECT name from CUSTOMER where id = ?", String.class, id));

}

Multiple data sources in the same test class

If you need more than one database instance in your test class, you should name them using the "withName" construct. If not set the rule builder will generate the name using the name of the test class

@Rule
public EmbeddedDatabaseRule embeddedDatabaseMysqlRule =
        EmbeddedDatabaseRule.builder().withName("db1").withMode("MySQL").build();

@Rule
public EmbeddedDatabaseRule embeddedDatabaseMsSqlServerRule =
        EmbeddedDatabaseRule.builder().withName("db2").withMode("MSSQLServer").build();

About

JUnit Rule for providing a H2 Embedded in-memory database for your tests

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%