[BAEL-2749] Adds DataSourceBaseDBTestCase

This commit is contained in:
Joao Esperancinha 2020-03-31 12:02:24 +02:00
parent e4f4f7110b
commit d5d1cc7d58
2 changed files with 52 additions and 6 deletions

View File

@ -0,0 +1,46 @@
package com.baeldung.dbunit;
import org.dbunit.DataSourceBasedDBTestCase;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.h2.jdbcx.JdbcDataSource;
import org.junit.Test;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import static org.assertj.core.api.Assertions.assertThat;
public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase {
@Override
protected DataSource getDataSource() {
JdbcDataSource dataSource = new JdbcDataSource();
dataSource.setURL(
"jdbc:h2:mem:default;DB_CLOSE_DELAY=-1;init=runscript from 'classpath:schema.sql'");
dataSource.setUser("sa");
dataSource.setPassword("sa");
return dataSource;
}
@Override
protected IDataSet getDataSet() throws Exception {
return new FlatXmlDataSetBuilder().build(getClass()
.getClassLoader()
.getResourceAsStream("data.xml"));
}
@Test
public void testSimpleDataSet() throws SQLException {
final Connection connection = getDataSource()
.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");
}
}

View File

@ -19,9 +19,9 @@ import static com.baeldung.dbunit.ConnectionSettings.PASSWORD;
import static com.baeldung.dbunit.ConnectionSettings.USER;
import static org.assertj.core.api.Assertions.assertThat;
public class SampleDbUnitTest extends DBTestCase {
public class DbUnitTest extends DBTestCase {
public SampleDbUnitTest(String name) {
public DbUnitTest(String name) {
super(name);
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, JDBC_DRIVER);
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, JDBC_URL);
@ -32,7 +32,7 @@ public class SampleDbUnitTest extends DBTestCase {
@Override
protected IDataSet getDataSet() throws Exception {
final InputStream is = SampleDbUnitTest.class.getClassLoader().getResourceAsStream("data.xml");
final InputStream is = DbUnitTest.class.getClassLoader().getResourceAsStream("data.xml");
return new FlatXmlDataSetBuilder().build(is);
}
@ -53,14 +53,14 @@ public class SampleDbUnitTest extends DBTestCase {
final ResultSet rs = connection.createStatement().executeQuery("select * from iTEMS where id = 1");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("title")).isEqualTo("Grey T-Shirt");
assertThat(rs.getString( "title")).isEqualTo("Grey T-Shirt");
}
@Test
public void testDelete() throws Exception {
final Connection connection = getConnection().getConnection();
final InputStream is = SampleDbUnitTest.class.getClassLoader().getResourceAsStream("items_exp_delete.xml");
final InputStream is = DbUnitTest.class.getClassLoader().getResourceAsStream("items_exp_delete.xml");
ITable expectedTable = (new FlatXmlDataSetBuilder().build(is)).getTable("items");
connection.createStatement().executeUpdate("delete from ITEMS where id = 2");
@ -75,7 +75,7 @@ public class SampleDbUnitTest extends DBTestCase {
public void testUpdate() throws Exception {
final Connection connection = getConnection().getConnection();
final InputStream is = SampleDbUnitTest.class.getClassLoader().getResourceAsStream("items_exp_rename.xml");
final InputStream is = DbUnitTest.class.getClassLoader().getResourceAsStream("items_exp_rename.xml");
ITable expectedTable = (new FlatXmlDataSetBuilder().build(is)).getTable("items");
connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1");