assertj-dbを依存に追加する
testCompile 'org.assertj:assertj-db:1.2.0'
テーブルの内容
ID,NAME 1,name 2,name_2
シンプルにテーブルの内容をアサートする例
Tableクラスを使うことでテーブルの内容全体に対してアサートを行なうことができます。final OracleDataSource source = new OracleDataSource();
// 接続先情報は省略
final Table.Order order = Table.Order.asc("id");
final Table table = new Table(source, "test_table", new Table.Order[] {order});
assertThat(table)
.column("id")
.value().isEqualTo(1L)
.value().isEqualTo(2L)
.column("name")
.value().isEqualTo("name")
.value().isEqualTo("name_2");
SQLの結果に対してアサート
Requestクラスを使ってSQLを実行し、その取得内容をアサートすることができます。final Request request = new Request(source, "select name from test_table where id = 2");
assertThat(request).column(0).value().isEqualTo("name_2");
テーブルの変更内容をアサートする
Changesクラスを使うことで、開始ポイントと終了ポイントを定義しその間の変更内容をアサートすることができます。この例では、1レコードの変更が正しいことをアサートしています。
final Table table = new Table(source, "test_table");
final Changes changes = new Changes(table);
changes.setStartPointNow();
try (final Connection connection = source.getConnection();
final PreparedStatement statement = connection.prepareStatement(
"update test_table set name = ? where id = 2")) {
statement.setString(1, "へんこうご");
statement.execute();
}
changes.setEndPointNow();
assertThat(changes)
.hasNumberOfChanges(1) // 1レコード変更されている
.ofModification().hasNumberOfChanges(1) // 変更内容は更新
.change()
.columnAmongTheModifiedOnes()
.hasColumnName("name")
.hasValues("name_2", "へんこうご"); // name_2からへんこうごに変更されていること