[BAEL-2749] Renaming test methods and running them with JUnit4
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user