Quick start

Suppose that the database contains this table MEMBERS :

ID NAME FIRSTNAME SURNAME BIRTHDATE SIZE

1

'Hewson'

'Paul David'

'Bono'

05-10-60

1.75

2

'Evans'

'David Howell'

'The Edge'

08-08-61

1.77

3

'Clayton'

'Adam'

 

03-13-60

1.78

4

'Mullen'

'Larry'

 

10-31-61

1.70

To quickly start using DataBase assertions, follow the steps below.

Add the assertj-db dependency to your project

Maven

<dependency>
  <groupId>org.assertj</groupId>
  <artifactId>assertj-db</artifactId>
  <version>3.0.0</version>
  <scope>test</scope>
</dependency>

Gradle

For Gradle users (using the Maven Central Repository)

testCompile("org.assertj:assertj-db:3.0.0")

Other dependency management tool

Check this page to find the relevant assertj db dependency declaration.

Statically import org.assertj.db.api.Assertions.assertThat

... and use your preferred IDE code completion after assertThat.

import static org.assertj.db.api.Assertions.assertThat;

import org.assertj.db.type.AssertDbConnection;
import org.assertj.db.type.AssertDbConnectionFactory;
import org.assertj.db.type.DateValue;
import org.assertj.db.type.Table;

  private final AssertDbConnection assertDbConnection = AssertDbConnectionFactory.of("jdbc:h2:mem:test", "sa", "").create();

    Table table = assertDbConnection.table("members").build();

    // Check column "name" values
    assertThat(table).column("name")
      .value().isEqualTo("Hewson")
      .value().isEqualTo("Evans")
      .value().isEqualTo("Clayton")
      .value().isEqualTo("Mullen");

    // Check row at index 1 (the second row) values
    assertThat(table).row(1)
      .value().isEqualTo(2)
      .value().isEqualTo("Evans")
      .value().isEqualTo("David Howell")
      .value().isEqualTo("The Edge")
      .value().isEqualTo(DateValue.of(1961, 8, 8))
      .value().isEqualTo(1.77);

In this simple example you can see many concepts of AssertJ-DB (the concepts are simple, but I advise you to take the time to get to know them well) :

  • The elements :

    • Table which represents a table in the database

    • Column which represents a column of the table

    • Row which represents a row of the table

    • Value which represents a value in a column or in a row

  • The navigation :

    • The first check, navigates from the table to the column called "name" (column("name") moves the assertion to the column), from this column to the first value (the first call of value() moves to the first value) and after that to each value (each call of value() moves to the next value of the column).

    • The second check, navigates from the table to the row with index 1 (row(1) moves the assertion to the row), from this row to the first value and after that to each value (value() calls have similar behavior for rows and columns).

  • DateValue : The preferred way to compare values with date, time and date/time is to use java.time.LocalDate, java.time.LocalTime, java.time.LocalDateTime. But for the backward compatibility, it’s always possible to use AssertJ-DB DateValue utilities.