[BAEL-2749] Test Code Completion
This commit is contained in:
parent
07947ce4d6
commit
906d72859f
|
@ -2,10 +2,15 @@ package com.baeldung.dbunit;
|
|||
|
||||
import org.dbunit.Assertion;
|
||||
import org.dbunit.DataSourceBasedDBTestCase;
|
||||
import org.dbunit.assertion.DiffCollectingFailureHandler;
|
||||
import org.dbunit.assertion.Difference;
|
||||
import org.dbunit.dataset.IDataSet;
|
||||
import org.dbunit.dataset.ITable;
|
||||
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
|
||||
import org.dbunit.operation.DatabaseOperation;
|
||||
import org.h2.jdbcx.JdbcDataSource;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
@ -14,6 +19,7 @@ import java.sql.ResultSet;
|
|||
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;
|
||||
|
||||
public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase {
|
||||
|
@ -34,6 +40,27 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase {
|
|||
.getResourceAsStream("data.xml"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected DatabaseOperation getSetUpOperation() {
|
||||
return DatabaseOperation.REFRESH;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected DatabaseOperation getTearDownOperation() {
|
||||
return DatabaseOperation.DELETE_ALL;
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSimpleDataSet() throws SQLException {
|
||||
final Connection connection = getDataSource()
|
||||
|
@ -56,7 +83,6 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase {
|
|||
Assertion.assertEquals(expectedTable, actualTable);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testAssertByQuery() throws Exception {
|
||||
IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(getClass()
|
||||
|
@ -70,7 +96,26 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase {
|
|||
ITable actualData = getConnection()
|
||||
.createQueryTable(
|
||||
"result_name",
|
||||
"SELECT * FROM CLIENTS WHERE id='2'");
|
||||
"SELECT * FROM CLIENTS WHERE last_name='Jansen'");
|
||||
Assertion.assertEqualsIgnoreCols(expectedTable, actualData, new String[]{"id"});
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void testMultipleFailures() throws Exception {
|
||||
IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(getClass().getClassLoader().getResourceAsStream("expected-multiple-failures.xml"));
|
||||
ITable expectedTable = expectedDataSet.getTable("ITEMS");
|
||||
Connection conn = getDataSource().getConnection();
|
||||
conn.createStatement().executeUpdate("INSERT INTO ITEMS (title, price) VALUES ('Battery', '1000000')");
|
||||
ITable actualData = getConnection().createDataSet().getTable("ITEMS");
|
||||
DiffCollectingFailureHandler collectingHandler = new DiffCollectingFailureHandler();
|
||||
Assertion.assertEquals(expectedTable, actualData, collectingHandler);
|
||||
if (!collectingHandler.getDiffList().isEmpty()) {
|
||||
String message = (String) collectingHandler.getDiffList().stream().map(d -> formatDifference((Difference) d)).collect(joining("\n"));
|
||||
// throw new AssertionError(message);
|
||||
}
|
||||
}
|
||||
|
||||
private static String formatDifference(Difference diff) {
|
||||
return "expected value in " + diff.getExpectedTable().getTableMetaData().getTableName() + "." + diff.getColumnName() + " row " + diff.getRowIndex() + ":" + diff.getExpectedValue() + ", but was: " + diff.getActualValue();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@ public class DbUnitTest extends DBTestCase {
|
|||
|
||||
@Override
|
||||
protected DatabaseOperation getTearDownOperation() {
|
||||
return DatabaseOperation.NONE;
|
||||
return DatabaseOperation.DELETE_ALL;
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.baeldung.dbunit;
|
||||
|
||||
import org.dbunit.Assertion;
|
||||
import org.dbunit.IDatabaseTester;
|
||||
import org.dbunit.JdbcDatabaseTester;
|
||||
import org.dbunit.dataset.IDataSet;
|
||||
|
@ -36,7 +37,7 @@ public class OldSchoolDbUnitTest {
|
|||
final JdbcDatabaseTester tester = new JdbcDatabaseTester(JDBC_DRIVER, JDBC_URL, USER, PASSWORD);
|
||||
tester.setDataSet(initDataSet());
|
||||
tester.setSetUpOperation(DatabaseOperation.REFRESH);
|
||||
tester.setTearDownOperation(DatabaseOperation.NONE);
|
||||
tester.setTearDownOperation(DatabaseOperation.DELETE_ALL);
|
||||
return tester;
|
||||
}
|
||||
|
||||
|
@ -65,6 +66,22 @@ public class OldSchoolDbUnitTest {
|
|||
assertThat(rs.getString("title")).isEqualTo("Grey T-Shirt");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoringProduced() throws Exception {
|
||||
final Connection connection = tester.getConnection().getConnection();
|
||||
final String[] excludedColumns = {"id", "produced"};
|
||||
final IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(getClass().getClassLoader()
|
||||
.getResourceAsStream("expected-ignoring-registered_at.xml"));
|
||||
final ITable expectedTable = DefaultColumnFilter.excludedColumnsTable(expectedDataSet.getTable("ITEMS"), excludedColumns);
|
||||
|
||||
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);
|
||||
|
||||
Assertion.assertEquals(expectedTable, actualTable);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelete() throws Exception {
|
||||
final Connection connection = tester.getConnection().getConnection();
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<dataset>
|
||||
<CLIENTS id='1' first_name='Charles' last_name='Xavier'/>
|
||||
|
||||
<ITEMS id='1' title='Grey T-Shirt' price='17.99' produced='2019-03-20'/>
|
||||
<ITEMS id='2' title='Fitted Hat' price='29.99' produced='2019-03-21'/>
|
||||
<ITEMS id='3' title='Backpack' price='54.99' produced='2019-03-22'/>
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<dataset>
|
||||
<ITEMS id='1' title='Grey T-Shirt' price='17.99' produced='2019-03-20'/>
|
||||
<ITEMS id='2' title='Fitted Hat' price='29.99' produced='2019-03-21'/>
|
||||
<ITEMS id='3' title='Backpack' price='54.99' produced='2019-03-22'/>
|
||||
<ITEMS id='4' title='Earrings' price='14.99' produced='2019-03-23'/>
|
||||
<ITEMS id='5' title='Socks' price='9.99'/>
|
||||
<ITEMS id='6' title='Necklace' price='199.99' produced='2019-03-23'/>
|
||||
</dataset>
|
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<dataset>
|
||||
<ITEMS id='1' title='Grey T-Shirt' price='17.99' produced='2019-03-20'/>
|
||||
<ITEMS id='2' title='Fitted Hat' price='29.99' produced='2019-03-21'/>
|
||||
<ITEMS id='3' title='Backpack' price='54.99' produced='2019-03-22'/>
|
||||
<ITEMS id='4' title='Earrings' price='14.99' produced='2019-03-23'/>
|
||||
<ITEMS id='5' title='Socks' price='9.99'/>
|
||||
<ITEMS id='6' title='Necklace' price='199.99' produced='2019-03-23'/>
|
||||
</dataset>
|
|
@ -1,5 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<dataset>
|
||||
<CLIENTS id='1' first_name='John' last_name='Jansen'/>
|
||||
<CLIENTS id='2' first_name='John' last_name='Jansen'/>
|
||||
</dataset>
|
|
@ -1,5 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<dataset>
|
||||
<ITEMS id='1' title='Grey T-Shirt' price='17.99' produced='2019-03-20'/>
|
||||
<ITEMS id='2' title='Fitted Hat' price='29.99' produced='2019-03-21'/>
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<dataset>
|
||||
<ITEMS id='1' title='Grey T-Shirt' price='17.99' produced='2019-03-20'/>
|
||||
<ITEMS id='3' title='Backpack' price='54.99' produced='2019-03-22'/>
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<dataset>
|
||||
<ITEMS id='1' title='Grey T-Shirt' price='17.99'/>
|
||||
<ITEMS id='3' title='Backpack' price='54.99'/>
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<dataset>
|
||||
<ITEMS id='1' title='new name' price='17.99' produced='2019-03-20'/>
|
||||
<ITEMS id='2' title='Fitted Hat' price='29.99' produced='2019-03-21'/>
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<dataset>
|
||||
<ITEMS id='1' title='new name' price='17.99'/>
|
||||
<ITEMS id='2' title='Fitted Hat' price='29.99'/>
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<dataset>
|
||||
<CLIENTS id='1' first_name='Charles' last_name='Xavier'/>
|
||||
<CLIENTS id='2' first_name='Scott' last_name='Summers'/>
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<dataset>
|
||||
<CLIENTS id='1' first_name='Charles' last_name='Xavier'/>
|
||||
<CLIENTS id='3' first_name='Jean' last_name='Grey'/>
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<dataset>
|
||||
<CLIENTS id='1' first_name='new name' last_name='Xavier'/>
|
||||
<CLIENTS id='2' first_name='Scott' last_name='Summers'/>
|
||||
|
|
Loading…
Reference in New Issue