From e98323d913d19af96119a9d9220b8eebda64c2fc Mon Sep 17 00:00:00 2001 From: Joao Esperancinha Date: Fri, 3 Apr 2020 08:51:46 +0200 Subject: [PATCH] [BAEL-2749] Renaming test methods and running them with JUnit4 --- .../baeldung/dbunit/DataSourceDBUnitTest.java | 63 +++++++++++-- .../java/com/baeldung/dbunit/DbUnitTest.java | 91 ------------------- .../baeldung/dbunit/OldSchoolDbUnitTest.java | 26 ++++-- .../dbunit/PrepAndExpectedDbUnitTest.java | 87 ------------------ .../jsonassert/JsonAssertUnitTest.java | 10 +- .../src/test/resources/dbunit/data.xml | 2 +- .../dbunit/items_exp_rename_no_produced.xml | 2 +- .../src/test/resources/dbunit/users.xml | 2 +- .../resources/dbunit/users_exp_delete.xml | 2 +- .../resources/dbunit/users_exp_rename.xml | 2 +- 10 files changed, 80 insertions(+), 207 deletions(-) delete mode 100644 libraries-testing/src/test/java/com/baeldung/dbunit/DbUnitTest.java delete mode 100644 libraries-testing/src/test/java/com/baeldung/dbunit/PrepAndExpectedDbUnitTest.java diff --git a/libraries-testing/src/test/java/com/baeldung/dbunit/DataSourceDBUnitTest.java b/libraries-testing/src/test/java/com/baeldung/dbunit/DataSourceDBUnitTest.java index 93503277b4..20b0337f4d 100644 --- a/libraries-testing/src/test/java/com/baeldung/dbunit/DataSourceDBUnitTest.java +++ b/libraries-testing/src/test/java/com/baeldung/dbunit/DataSourceDBUnitTest.java @@ -14,6 +14,8 @@ import org.junit.Before; import org.junit.Test; import org.junit.platform.commons.logging.Logger; import org.junit.platform.commons.logging.LoggerFactory; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; import java.io.InputStream; import java.sql.Connection; @@ -23,7 +25,9 @@ import java.sql.SQLException; import static com.baeldung.dbunit.ConnectionSettings.JDBC_URL; import static java.util.stream.Collectors.joining; import static org.assertj.core.api.Assertions.assertThat; +import static org.dbunit.Assertion.assertEqualsIgnoreCols; +@RunWith(JUnit4.class) public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase { private static final Logger logger = LoggerFactory.getLogger(DataSourceDBUnitTest.class); @@ -65,7 +69,7 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase { } @Test - public void testSimpleDataSet() throws SQLException { + public void givenDataSet_whenSelect_thenFirstTitleIsGreyTShirt() throws SQLException { final Connection connection = getDataSource().getConnection(); final ResultSet rs = connection.createStatement().executeQuery("select * from ITEMS where id = 1"); @@ -75,7 +79,7 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase { } @Test - public void testEmptySchema() throws Exception { + public void givenDataSetEmptySchema_whenDataSetCreated_thenTablesAreEqual() throws Exception { final IDataSet expectedDataSet = getDataSet(); final ITable expectedTable = expectedDataSet.getTable("CLIENTS"); final IDataSet databaseDataSet = getConnection().createDataSet(); @@ -84,33 +88,74 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase { } @Test - public void testAssertByQuery() throws Exception { + public void givenDataSet_whenInsert_thenTableHasNewClient() throws Exception { try (final InputStream is = getClass().getClassLoader().getResourceAsStream("dbunit/expected-user.xml")) { final IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is); final ITable expectedTable = expectedDataSet.getTable("CLIENTS"); final Connection conn = getDataSource().getConnection(); - conn.createStatement().executeUpdate("INSERT INTO CLIENTS (first_name, last_name) VALUES ('John', 'Jansen')"); - final ITable actualData = getConnection().createQueryTable("result_name", "SELECT * FROM CLIENTS WHERE last_name='Jansen'"); + conn.createStatement() + .executeUpdate( + "INSERT INTO CLIENTS (first_name, last_name) VALUES ('John', 'Jansen')"); + final ITable actualData = getConnection() + .createQueryTable( + "result_name", + "SELECT * FROM CLIENTS WHERE last_name='Jansen'"); - Assertion.assertEqualsIgnoreCols(expectedTable, actualData, new String[] { "id" }); + assertEqualsIgnoreCols(expectedTable, actualData, new String[] { "id" }); } } @Test - public void testMultipleFailures() throws Exception { + public void givenDataSet_whenDelete_thenItemIsDeleted() throws Exception { + final Connection connection = getConnection().getConnection(); + + try (final InputStream is = DataSourceDBUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_delete.xml")) { + ITable expectedTable = (new FlatXmlDataSetBuilder().build(is)).getTable("ITEMS"); + + connection.createStatement().executeUpdate("delete from ITEMS where id = 2"); + + final IDataSet databaseDataSet = getConnection().createDataSet(); + ITable actualTable = databaseDataSet.getTable("ITEMS"); + + Assertion.assertEquals(expectedTable, actualTable); + } + } + + @Test + public void givenDataSet_whenUpdate_thenItemHasNewName() throws Exception { + final Connection connection = getConnection().getConnection(); + + try (final InputStream is = DataSourceDBUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_rename.xml")) { + ITable expectedTable = (new FlatXmlDataSetBuilder().build(is)).getTable("ITEMS"); + + connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1"); + + final IDataSet databaseDataSet = getConnection().createDataSet(); + ITable actualTable = databaseDataSet.getTable("ITEMS"); + + Assertion.assertEquals(expectedTable, actualTable); + } + } + + @Test + public void givenDataSet_whenInsertUnexpectedData_thenFailOnAllUnexpectedValues() throws Exception { try (final InputStream is = getClass().getClassLoader().getResourceAsStream("dbunit/expected-multiple-failures.xml")) { final IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is); final ITable expectedTable = expectedDataSet.getTable("ITEMS"); final Connection conn = getDataSource().getConnection(); final DiffCollectingFailureHandler collectingHandler = new DiffCollectingFailureHandler(); - conn.createStatement().executeUpdate("INSERT INTO ITEMS (title, price) VALUES ('Battery', '1000000')"); + conn.createStatement().executeUpdate( + "INSERT INTO ITEMS (title, price) VALUES ('Battery', '1000000')"); final ITable actualData = getConnection().createDataSet().getTable("ITEMS"); Assertion.assertEquals(expectedTable, actualData, collectingHandler); if (!collectingHandler.getDiffList().isEmpty()) { - String message = (String) collectingHandler.getDiffList().stream().map(d -> formatDifference((Difference) d)).collect(joining("\n")); + String message = (String) collectingHandler + .getDiffList() + .stream() + .map(d -> formatDifference((Difference) d)).collect(joining("\n")); logger.error(() -> message); } } diff --git a/libraries-testing/src/test/java/com/baeldung/dbunit/DbUnitTest.java b/libraries-testing/src/test/java/com/baeldung/dbunit/DbUnitTest.java deleted file mode 100644 index da4ac54f12..0000000000 --- a/libraries-testing/src/test/java/com/baeldung/dbunit/DbUnitTest.java +++ /dev/null @@ -1,91 +0,0 @@ -package com.baeldung.dbunit; - -import org.dbunit.Assertion; -import org.dbunit.DBTestCase; -import org.dbunit.PropertiesBasedJdbcDatabaseTester; -import org.dbunit.dataset.IDataSet; -import org.dbunit.dataset.ITable; -import org.dbunit.dataset.xml.FlatXmlDataSetBuilder; -import org.dbunit.operation.DatabaseOperation; -import org.junit.Test; - -import java.io.InputStream; -import java.sql.Connection; -import java.sql.ResultSet; - -import static com.baeldung.dbunit.ConnectionSettings.JDBC_DRIVER; -import static com.baeldung.dbunit.ConnectionSettings.JDBC_URL; -import static com.baeldung.dbunit.ConnectionSettings.PASSWORD; -import static com.baeldung.dbunit.ConnectionSettings.USER; -import static org.assertj.core.api.Assertions.assertThat; - -public class DbUnitTest extends DBTestCase { - - public DbUnitTest(String name) { - super(name); - System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, JDBC_DRIVER); - System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, JDBC_URL); - System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, USER); - System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, PASSWORD); - } - - @Override - protected IDataSet getDataSet() throws Exception { - try (final InputStream is = DbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/data.xml")) { - return new FlatXmlDataSetBuilder().build(is); - } - } - - @Override - protected DatabaseOperation getSetUpOperation() { - return DatabaseOperation.REFRESH; - } - - @Override - protected DatabaseOperation getTearDownOperation() { - return DatabaseOperation.DELETE_ALL; - } - - @Test - public void testSelect() throws Exception { - final Connection connection = getConnection().getConnection(); - - final ResultSet rs = connection.createStatement().executeQuery("select * from ITEMS where id = 1"); - - assertThat(rs.next()).isTrue(); - assertThat(rs.getString("title")).isEqualTo("Grey T-Shirt"); - } - - @Test - public void testDelete() throws Exception { - final Connection connection = getConnection().getConnection(); - - try (final InputStream is = DbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_delete.xml")) { - ITable expectedTable = (new FlatXmlDataSetBuilder().build(is)).getTable("ITEMS"); - - connection.createStatement().executeUpdate("delete from ITEMS where id = 2"); - - final IDataSet databaseDataSet = getConnection().createDataSet(); - ITable actualTable = databaseDataSet.getTable("ITEMS"); - - Assertion.assertEquals(expectedTable, actualTable); - } - } - - @Test - public void testUpdate() throws Exception { - final Connection connection = getConnection().getConnection(); - - try (final InputStream is = DbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_rename.xml")) { - ITable expectedTable = (new FlatXmlDataSetBuilder().build(is)).getTable("ITEMS"); - - connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1"); - - final IDataSet databaseDataSet = getConnection().createDataSet(); - ITable actualTable = databaseDataSet.getTable("ITEMS"); - - Assertion.assertEquals(expectedTable, actualTable); - } - } - -} diff --git a/libraries-testing/src/test/java/com/baeldung/dbunit/OldSchoolDbUnitTest.java b/libraries-testing/src/test/java/com/baeldung/dbunit/OldSchoolDbUnitTest.java index a7821102dc..6243af9676 100644 --- a/libraries-testing/src/test/java/com/baeldung/dbunit/OldSchoolDbUnitTest.java +++ b/libraries-testing/src/test/java/com/baeldung/dbunit/OldSchoolDbUnitTest.java @@ -12,6 +12,8 @@ import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; import java.io.InputStream; import java.sql.Connection; @@ -24,6 +26,7 @@ import static com.baeldung.dbunit.ConnectionSettings.USER; import static org.assertj.core.api.Assertions.assertThat; import static org.dbunit.Assertion.assertEquals; +@RunWith(JUnit4.class) public class OldSchoolDbUnitTest { private static IDatabaseTester tester = null; @@ -58,7 +61,7 @@ public class OldSchoolDbUnitTest { } @Test - public void testSelect() throws Exception { + public void givenDataSet_whenSelect_thenFirstTitleIsGreyTShirt() throws Exception { final Connection connection = tester.getConnection().getConnection(); final ResultSet rs = connection.createStatement().executeQuery("select * from ITEMS where id = 1"); @@ -68,24 +71,27 @@ public class OldSchoolDbUnitTest { } @Test - public void testIgnoringProduced() throws Exception { + public void givenDataSet_whenInsert_thenGetResultsAreStillEqualIfIgnoringColumnsWithDifferentProduced() throws Exception { final Connection connection = tester.getConnection().getConnection(); final String[] excludedColumns = { "id", "produced" }; try (final InputStream is = getClass().getClassLoader().getResourceAsStream("dbunit/expected-ignoring-registered_at.xml")) { final IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is); - final ITable expectedTable = DefaultColumnFilter.excludedColumnsTable(expectedDataSet.getTable("ITEMS"), excludedColumns); + final ITable expectedTable = DefaultColumnFilter.excludedColumnsTable( + expectedDataSet.getTable("ITEMS"), excludedColumns); - connection.createStatement().executeUpdate("INSERT INTO ITEMS (title, price, produced) VALUES('Necklace', 199.99, now())"); + connection.createStatement().executeUpdate( + "INSERT INTO ITEMS (title, price, produced) VALUES('Necklace', 199.99, now())"); final IDataSet databaseDataSet = tester.getConnection().createDataSet(); - final ITable actualTable = DefaultColumnFilter.excludedColumnsTable(databaseDataSet.getTable("ITEMS"), excludedColumns); + final ITable actualTable = DefaultColumnFilter.excludedColumnsTable( + databaseDataSet.getTable("ITEMS"), excludedColumns); Assertion.assertEquals(expectedTable, actualTable); } } @Test - public void testDelete() throws Exception { + public void givenDataSet_whenDelete_thenItemIsRemoved() throws Exception { final Connection connection = tester.getConnection().getConnection(); try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_delete.xml")) { @@ -101,7 +107,7 @@ public class OldSchoolDbUnitTest { } @Test - public void testDeleteWithExcludedColumns() throws Exception { + public void givenDataSet_whenDelete_thenItemIsRemovedAndResultsEqualIfProducedIsIgnored() throws Exception { final Connection connection = tester.getConnection().getConnection(); try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_delete_no_produced.xml")) { @@ -118,7 +124,7 @@ public class OldSchoolDbUnitTest { } @Test - public void testUpdate() throws Exception { + public void givenDataSet_whenUpdate_thenItemHasNewName() throws Exception { final Connection connection = tester.getConnection().getConnection(); try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_rename.xml")) { @@ -134,18 +140,18 @@ public class OldSchoolDbUnitTest { } @Test - public void testUpdateWithExcludedColumns() throws Exception { + public void givenDataSet_whenUpdateWithNoProduced_thenItemHasNewName() throws Exception { final Connection connection = tester.getConnection().getConnection(); try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_rename_no_produced.xml")) { ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS"); + expectedTable = DefaultColumnFilter.excludedColumnsTable(expectedTable, new String[] { "produced" }); connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1"); final IDataSet databaseDataSet = tester.getConnection().createDataSet(); ITable actualTable = databaseDataSet.getTable("ITEMS"); actualTable = DefaultColumnFilter.excludedColumnsTable(actualTable, new String[] { "produced" }); - assertEquals(expectedTable, actualTable); } } diff --git a/libraries-testing/src/test/java/com/baeldung/dbunit/PrepAndExpectedDbUnitTest.java b/libraries-testing/src/test/java/com/baeldung/dbunit/PrepAndExpectedDbUnitTest.java deleted file mode 100644 index 039dfd1639..0000000000 --- a/libraries-testing/src/test/java/com/baeldung/dbunit/PrepAndExpectedDbUnitTest.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.baeldung.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; -import org.dbunit.util.fileloader.DataFileLoader; -import org.dbunit.util.fileloader.FlatXmlDataFileLoader; -import org.junit.Test; - -import java.io.InputStream; -import java.sql.Connection; -import java.sql.ResultSet; - -import static com.baeldung.dbunit.ConnectionSettings.JDBC_DRIVER; -import static com.baeldung.dbunit.ConnectionSettings.JDBC_URL; -import static com.baeldung.dbunit.ConnectionSettings.PASSWORD; -import static com.baeldung.dbunit.ConnectionSettings.USER; -import static org.assertj.core.api.Assertions.assertThat; - -public class PrepAndExpectedDbUnitTest extends DefaultPrepAndExpectedTestCase { - - @Override - public void setUp() throws Exception { - setDatabaseTester(initDatabaseTester()); - setDataFileLoader(initDataFileLoader()); - super.setUp(); - } - - private IDatabaseTester initDatabaseTester() throws Exception { - final JdbcDatabaseTester tester = new JdbcDatabaseTester(JDBC_DRIVER, JDBC_URL, USER, PASSWORD); - tester.setDataSet(initDataSet()); - tester.setSetUpOperation(DatabaseOperation.REFRESH); - return tester; - } - - private IDataSet initDataSet() throws Exception { - try (final InputStream is = getClass().getClassLoader().getResourceAsStream("dbunit/data.xml")) { - return new FlatXmlDataSetBuilder().build(is); - } - } - - private DataFileLoader initDataFileLoader() { - return new FlatXmlDataFileLoader(); - } - - @Test - public void testSelect() throws Exception { - final Connection connection = getConnection().getConnection(); - final VerifyTableDefinition[] verifyTables = { new VerifyTableDefinition("CLIENTS", new String[] {}) }; - final String[] prepDataFiles = { "/dbunit/users.xml" }; - final String[] expectedDataFiles = { "/dbunit/users.xml" }; - final PrepAndExpectedTestCaseSteps testSteps = () -> connection.createStatement().executeQuery("select * from CLIENTS where id = 1"); - - final ResultSet rs = (ResultSet) super.runTest(verifyTables, prepDataFiles, expectedDataFiles, testSteps); - - assertThat(rs.next()).isTrue(); - assertThat(rs.getString("last_name")).isEqualTo("Xavier"); - } - - @Test - public void testUpdate() throws Exception { - final Connection connection = getConnection().getConnection(); - final VerifyTableDefinition[] verifyTables = { new VerifyTableDefinition("CLIENTS", new String[] {}) }; // define tables to verify - final String[] prepDataFiles = { "/dbunit/users.xml" }; - final String[] expectedDataFiles = { "/dbunit/users_exp_rename.xml" }; - final PrepAndExpectedTestCaseSteps testSteps = () -> connection.createStatement().executeUpdate("update CLIENTS set first_name = 'new name' where id = 1"); - - super.runTest(verifyTables, prepDataFiles, expectedDataFiles, testSteps); - } - - @Test - public void testDelete() throws Exception { - final Connection connection = getConnection().getConnection(); - final VerifyTableDefinition[] verifyTables = { new VerifyTableDefinition("CLIENTS", new String[] {}) }; - final String[] prepDataFiles = { "/dbunit/users.xml" }; - final String[] expectedDataFiles = { "/dbunit/users_exp_delete.xml" }; - final PrepAndExpectedTestCaseSteps testSteps = () -> connection.createStatement().executeUpdate("delete from CLIENTS where id = 2"); - - super.runTest(verifyTables, prepDataFiles, expectedDataFiles, testSteps); - } - -} diff --git a/libraries-testing/src/test/java/com/baeldung/jsonassert/JsonAssertUnitTest.java b/libraries-testing/src/test/java/com/baeldung/jsonassert/JsonAssertUnitTest.java index ce9638c4af..822468e91f 100644 --- a/libraries-testing/src/test/java/com/baeldung/jsonassert/JsonAssertUnitTest.java +++ b/libraries-testing/src/test/java/com/baeldung/jsonassert/JsonAssertUnitTest.java @@ -78,10 +78,10 @@ public class JsonAssertUnitTest { @Test public void givenArray_whenComparing_thenOrderMustMatchForStrict() throws JSONException { - String result = "[Alex, Barbera, Charlie, Xavier]"; - JSONAssert.assertEquals("[Charlie, Alex, Xavier, Barbera]", result, JSONCompareMode.LENIENT); - JSONAssert.assertEquals("[Alex, Barbera, Charlie, Xavier]", result, JSONCompareMode.STRICT); - JSONAssert.assertNotEquals("[Charlie, Alex, Xavier, Barbera]", result, JSONCompareMode.STRICT); + String result = "[Alex, Barbera, Charlie, Wolf]"; + JSONAssert.assertEquals("[Charlie, Alex, Wolf, Barbera]", result, JSONCompareMode.LENIENT); + JSONAssert.assertEquals("[Alex, Barbera, Charlie, Wolf]", result, JSONCompareMode.STRICT); + JSONAssert.assertNotEquals("[Charlie, Alex, Wolf, Barbera]", result, JSONCompareMode.STRICT); } @Test @@ -94,7 +94,7 @@ public class JsonAssertUnitTest { @Test public void whenComparingSizeOfArray_thenPass() throws JSONException { - String names = "{names:[Alex, Barbera, Charlie, Xavier]}"; + String names = "{names:[Alex, Barbera, Charlie, Wolf]}"; JSONAssert.assertEquals("{names:[4]}", names, new ArraySizeComparator(JSONCompareMode.LENIENT)); } diff --git a/libraries-testing/src/test/resources/dbunit/data.xml b/libraries-testing/src/test/resources/dbunit/data.xml index 290cc36890..4865dec54c 100644 --- a/libraries-testing/src/test/resources/dbunit/data.xml +++ b/libraries-testing/src/test/resources/dbunit/data.xml @@ -1,6 +1,6 @@ - + diff --git a/libraries-testing/src/test/resources/dbunit/items_exp_rename_no_produced.xml b/libraries-testing/src/test/resources/dbunit/items_exp_rename_no_produced.xml index b42d3804fa..4f14b17113 100644 --- a/libraries-testing/src/test/resources/dbunit/items_exp_rename_no_produced.xml +++ b/libraries-testing/src/test/resources/dbunit/items_exp_rename_no_produced.xml @@ -1,6 +1,6 @@ - + diff --git a/libraries-testing/src/test/resources/dbunit/users.xml b/libraries-testing/src/test/resources/dbunit/users.xml index 9ac3909bc5..f04943c4cc 100644 --- a/libraries-testing/src/test/resources/dbunit/users.xml +++ b/libraries-testing/src/test/resources/dbunit/users.xml @@ -1,6 +1,6 @@ - + diff --git a/libraries-testing/src/test/resources/dbunit/users_exp_delete.xml b/libraries-testing/src/test/resources/dbunit/users_exp_delete.xml index 2fe97ae6f6..20a2f2f1a7 100644 --- a/libraries-testing/src/test/resources/dbunit/users_exp_delete.xml +++ b/libraries-testing/src/test/resources/dbunit/users_exp_delete.xml @@ -1,5 +1,5 @@ - + diff --git a/libraries-testing/src/test/resources/dbunit/users_exp_rename.xml b/libraries-testing/src/test/resources/dbunit/users_exp_rename.xml index 95682118bb..1ab6cf53b8 100644 --- a/libraries-testing/src/test/resources/dbunit/users_exp_rename.xml +++ b/libraries-testing/src/test/resources/dbunit/users_exp_rename.xml @@ -1,6 +1,6 @@ - +