JDBDT

Maven Central GitHub release Build status AppVeyor build status

JDBDT (Java DataBase Delta Testing) is an open-source Java library for testing (SQL-based) database applications. The library is designed for automation of database setup and validation in test code. JDBDT is compact and has no third-party library dependencies (it just the Java 8 SE API internally), making it also easy and lightweight to integrate.

Compared to existing database testing frameworks, the main conceptual novelty is the possibility of using δ-assertions. For details, you may browse the reference documentation available in this site, along with the Javadoc for the JDBDT API and the JDBDT tutorial.

In a nutshell, the main features are as follows:

  • The core functionality is exposed by a simple API facade.
import static org.jdbdt.JDBDT.*;
DB db = database("jdbc:myFaveDBEngine://myDB");

Table userTable = 
  table("USER")
 .columns("ID", "LOGIN", "NAME", "PASSWORD", "CREATED")
 .build(db);

Query idQuery = 
  select("LOGIN", "NAME")
 .from("USER")
 .where("ID = ?")
 .arguments(userId)
 .build(db);
  • Data sets are defined programmatically, without need to maintain external “data files”, for instance using data set builders
DataSet data = 
   builder(t)
  .sequence("ID", 1) // 1, 2, 3, ...
  .sequence("LOGIN", "harry", "mark", "john")
  .sequence("NAME", "Harry H", "Mark M", "John J")
  .sequence("PASSWORD", i -> "password " + i , 1)
  .random("CREATED", Date.valueOf("2015-01-01"), Date.valueOf("2015-12-31"))
  .generate(3) // generate 3 rows, 
  .sequence("LOGIN", i -> "guest_" + i, 4)  // "user_4", "user_5", ...
  .sequence("NAME", i -> "Guest User " + i, 4) // "Guest User 4", ...
  .value("password", "samePasswordForAllGuests") 
  .generate(6) // 6 more rows keeping ID sequence and CREATED random filler
  .data();   
  • Setup methods can be used to define database contents, for instance to populate tables, clear them, setting & restoring save points, …, e.g.
static Table theTable ;
static DataSet theinitialStata; 

@BeforeClass
public void globalSetup() {
  theTable = ... ;
  theInitialData = ...
}

@Before
public void perTestSetup() {
   populateIfChanged(initialData);
}
@Test
public void testUserInsertion() {
  User uJohn = ...;
  DataSet newRow = 
     data(theTable)
    .row(999, "john", "John", "jpass", Date.valueOf("2016-01-01"));
  sut.insertUser( uJohn ); 
  // Verify the insertion; assertion fails if other changes are detected
  assertInserted(newRow); 
}

@Test
public void testHarmlessQuery() {
  User u = sut.getUser("john");
  ... // standard assertions
  assertUnchanged(theTable); // no delta, query is really harmless!
}
<jdbdt-log-message time="..." version="...">
...
  <delta-assertion>
    <expected>
      <old-data count="0"/>
      <new-data count="0"/>
    </expected>
    <errors>
      <old-data>
        <expected count="0"/>
        <actual count="1">
          <row>
            <column java-type="java.lang.String" label="LOGIN">linus</column>
            <column java-type="java.lang.String" label="NAME">Linus Torvalds</column>
            <column java-type="java.lang.String" label="PASSWORD">linux</column>
            <column java-type="java.sql.Date" label="CREATED">2015-01-01</column>
          </row>
        </actual>
      </old-data>
      <new-data>
        <expected count="0"/>
        <actual count="0"/>
      </new-data>
    </errors>
  </delta-assertion>
</jdbdt-log-message>

Contribute

The code is hosted at GitHub. Please use the issue tracker to report bugs or propose new features.

Installation

Prerequisite: JDBDT requires Java 8, it will not work with earlier Java versions.

Maven Central

JDBDT is available from Maven Central.

Maven setup

<dependency>
    <groupId>org.jdbdt</groupId>
    <artifactId>jdbdt</artifactId>
    <version>1.0.0</version>
</dependency>

Gradle setup

 compile 'org.jdbdt:jdbdt:1.0.0'

Setup instructions for other build systems are available here .

GitHub

JDBDT release artifacts are also available at GitHub.

Snapshot builds

To compile and install the latest snapshot from scratch, use the following commands:

git clone git@github.com:edrdo/jdbdt.git
cd jdbdt
mvn install