[BAEL-2749] Introduces Assertj and uses builder pattern

This commit is contained in:
Joao Esperancinha 2020-03-29 11:37:19 +02:00
parent aaffd9614f
commit a65c5bf202
3 changed files with 45 additions and 11 deletions

View File

@ -3,6 +3,16 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>dbunit</artifactId> <artifactId>dbunit</artifactId>
<version>1.0</version> <version>1.0</version>
<properties>
<h2.version>1.4.200</h2.version>
<maven-compiler-plugin.target>1.8</maven-compiler-plugin.target>
<maven-compiler-plugin.source>1.8</maven-compiler-plugin.source>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<assertj-core.version>3.14.0</assertj-core.version>
<dbunit.version>2.6.0</dbunit.version>
</properties>
<name>dbunit</name> <name>dbunit</name>
<parent> <parent>
@ -22,14 +32,30 @@
<dependency> <dependency>
<groupId>com.h2database</groupId> <groupId>com.h2database</groupId>
<artifactId>h2</artifactId> <artifactId>h2</artifactId>
<version>1.4.197</version> <version>${h2.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<properties> <build>
<!-- testing --> <plugins>
<dbunit.version>2.6.0</dbunit.version> <plugin>
</properties> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven-compiler-plugin.source}</source>
<target>${maven-compiler-plugin.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project> </project>

View File

@ -6,6 +6,7 @@ import org.dbunit.JdbcDatabaseTester;
import org.dbunit.dataset.IDataSet; import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ITable; import org.dbunit.dataset.ITable;
import org.dbunit.dataset.xml.FlatXmlDataSet; import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.dbunit.operation.DatabaseOperation; import org.dbunit.operation.DatabaseOperation;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
@ -16,6 +17,7 @@ import java.io.InputStream;
import java.sql.Connection; import java.sql.Connection;
import java.sql.ResultSet; import java.sql.ResultSet;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -74,7 +76,7 @@ public class OldSchoolDbUnitTest {
final Connection connection = tester.getConnection().getConnection(); final Connection connection = tester.getConnection().getConnection();
final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("items_exp_delete.xml"); 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"}); //expectedTable = DefaultColumnFilter.excludedColumnsTable(expectedTable, new String[]{"produced"});
// Act // Act
@ -94,7 +96,7 @@ public class OldSchoolDbUnitTest {
final Connection connection = tester.getConnection().getConnection(); final Connection connection = tester.getConnection().getConnection();
final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("items_exp_rename.xml"); 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"}); //expectedTable = DefaultColumnFilter.excludedColumnsTable(expectedTable, new String[]{"produced"});
// Act // Act
@ -105,7 +107,7 @@ public class OldSchoolDbUnitTest {
ITable actualTable = databaseDataSet.getTable("items"); ITable actualTable = databaseDataSet.getTable("items");
//actualTable = DefaultColumnFilter.excludedColumnsTable(actualTable, new String[]{"produced"}); //actualTable = DefaultColumnFilter.excludedColumnsTable(actualTable, new String[]{"produced"});
Assertion.assertEquals(expectedTable, actualTable); assertThat(actualTable).isEqualTo(actualTable);
} }
} }

View File

@ -1,6 +1,10 @@
package com.baeldung.dbunit; 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.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder; import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.dbunit.operation.DatabaseOperation; import org.dbunit.operation.DatabaseOperation;
@ -12,6 +16,8 @@ import java.io.InputStream;
import java.sql.Connection; import java.sql.Connection;
import java.sql.ResultSet; import java.sql.ResultSet;
import static org.assertj.core.api.Assertions.assertThat;
public class PrepAndExpectedDbUnitTest extends DefaultPrepAndExpectedTestCase { public class PrepAndExpectedDbUnitTest extends DefaultPrepAndExpectedTestCase {
private static final String JDBC_DRIVER = org.h2.Driver.class.getName(); 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'"; 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); final ResultSet rs = (ResultSet) super.runTest(verifyTables, prepDataFiles, expectedDataFiles, testSteps);
// or place assertions at the end // or place assertions at the end
assertTrue(rs.next()); assertThat(rs.next()).isTrue();
assertEquals("Xavier", rs.getString("last_name")); assertThat(rs.getString("last_name")).isEqualTo("Xavier");
} }
@Test @Test