From a65c5bf202c3326514e7d6c1408bd9e445c06fd9 Mon Sep 17 00:00:00 2001 From: Joao Esperancinha Date: Sun, 29 Mar 2020 11:37:19 +0200 Subject: [PATCH] [BAEL-2749] Introduces Assertj and uses builder pattern --- dbunit/pom.xml | 36 ++++++++++++++++--- .../baeldung/dbunit/OldSchoolDbUnitTest.java | 8 +++-- .../dbunit/PrepAndExpectedDbUnitTest.java | 12 +++++-- 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/dbunit/pom.xml b/dbunit/pom.xml index b77b15f2d4..ab0befcf4c 100644 --- a/dbunit/pom.xml +++ b/dbunit/pom.xml @@ -3,6 +3,16 @@ 4.0.0 dbunit 1.0 + + 1.4.200 + + 1.8 + 1.8 + 3.8.1 + + 3.14.0 + 2.6.0 + dbunit @@ -22,14 +32,30 @@ com.h2database h2 - 1.4.197 + ${h2.version} + test + + + + org.assertj + assertj-core + ${assertj-core.version} test - - - 2.6.0 - + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${maven-compiler-plugin.source} + ${maven-compiler-plugin.target} + + + + diff --git a/dbunit/src/test/java/com/baeldung/dbunit/OldSchoolDbUnitTest.java b/dbunit/src/test/java/com/baeldung/dbunit/OldSchoolDbUnitTest.java index a703863614..2a2f25971b 100644 --- a/dbunit/src/test/java/com/baeldung/dbunit/OldSchoolDbUnitTest.java +++ b/dbunit/src/test/java/com/baeldung/dbunit/OldSchoolDbUnitTest.java @@ -6,6 +6,7 @@ import org.dbunit.JdbcDatabaseTester; import org.dbunit.dataset.IDataSet; import org.dbunit.dataset.ITable; import org.dbunit.dataset.xml.FlatXmlDataSet; +import org.dbunit.dataset.xml.FlatXmlDataSetBuilder; import org.dbunit.operation.DatabaseOperation; import org.junit.After; import org.junit.Before; @@ -16,6 +17,7 @@ import java.io.InputStream; import java.sql.Connection; import java.sql.ResultSet; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -74,7 +76,7 @@ public class OldSchoolDbUnitTest { final Connection connection = tester.getConnection().getConnection(); final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("items_exp_delete.xml"); - ITable expectedTable = (new FlatXmlDataSet(is)).getTable("items"); + ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("items"); //expectedTable = DefaultColumnFilter.excludedColumnsTable(expectedTable, new String[]{"produced"}); // Act @@ -94,7 +96,7 @@ public class OldSchoolDbUnitTest { final Connection connection = tester.getConnection().getConnection(); final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("items_exp_rename.xml"); - ITable expectedTable = (new FlatXmlDataSet(is)).getTable("items"); + ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("items"); //expectedTable = DefaultColumnFilter.excludedColumnsTable(expectedTable, new String[]{"produced"}); // Act @@ -105,7 +107,7 @@ public class OldSchoolDbUnitTest { ITable actualTable = databaseDataSet.getTable("items"); //actualTable = DefaultColumnFilter.excludedColumnsTable(actualTable, new String[]{"produced"}); - Assertion.assertEquals(expectedTable, actualTable); + assertThat(actualTable).isEqualTo(actualTable); } } diff --git a/dbunit/src/test/java/com/baeldung/dbunit/PrepAndExpectedDbUnitTest.java b/dbunit/src/test/java/com/baeldung/dbunit/PrepAndExpectedDbUnitTest.java index c3882d5eab..c322cf0ae3 100644 --- a/dbunit/src/test/java/com/baeldung/dbunit/PrepAndExpectedDbUnitTest.java +++ b/dbunit/src/test/java/com/baeldung/dbunit/PrepAndExpectedDbUnitTest.java @@ -1,6 +1,10 @@ package com.baeldung.dbunit; -import org.dbunit.*; +import org.dbunit.DefaultPrepAndExpectedTestCase; +import org.dbunit.IDatabaseTester; +import org.dbunit.JdbcDatabaseTester; +import org.dbunit.PrepAndExpectedTestCaseSteps; +import org.dbunit.VerifyTableDefinition; import org.dbunit.dataset.IDataSet; import org.dbunit.dataset.xml.FlatXmlDataSetBuilder; import org.dbunit.operation.DatabaseOperation; @@ -12,6 +16,8 @@ import java.io.InputStream; import java.sql.Connection; import java.sql.ResultSet; +import static org.assertj.core.api.Assertions.assertThat; + public class PrepAndExpectedDbUnitTest extends DefaultPrepAndExpectedTestCase { private static final String JDBC_DRIVER = org.h2.Driver.class.getName(); private static final String JDBC_URL = "jdbc:h2:mem:default;DB_CLOSE_DELAY=-1;init=runscript from 'classpath:schema.sql'"; @@ -63,8 +69,8 @@ public class PrepAndExpectedDbUnitTest extends DefaultPrepAndExpectedTestCase { final ResultSet rs = (ResultSet) super.runTest(verifyTables, prepDataFiles, expectedDataFiles, testSteps); // or place assertions at the end - assertTrue(rs.next()); - assertEquals("Xavier", rs.getString("last_name")); + assertThat(rs.next()).isTrue(); + assertThat(rs.getString("last_name")).isEqualTo("Xavier"); } @Test