Java Reference
In-Depth Information
Listing 17.13
user-reverted.xml, where the first line is incomplete
<?xml version="1.0"?>
<dataset>
<users id="2" username="TheStranger"/>
<users id="1" username="ElDuderino"
first_name="Jeffrey" last_name="Lebowsky" />
</dataset>
Now let's write a test case (listing 17.14) that does the following:
It uses the first dataset (user-ok.xml) to populate the database. Because the sec-
ond line doesn't have the first_name and last_name attributes, DbUnit inserts
NULL in the equivalent database columns.
1
Then it compares the database contents with the contents of both datasets
(user-ok.xml and user-reverted.xml).
2
Because both datasets contain the same lines (but in different order), both tests
should pass, but the user-reverted.xml assertion fails, complaining that the dataset
expected two columns but the database contained four:
junit.framework.ComparisonFailure: column count
(table=users, expectedColCount=2, actualColCount=4) expected:
<[[id, username]]> but was:<[[FIRST_NAME, ID, LAST_NAME, USERNAME]]>
at org.dbunit.Assertion.assertEquals(Assertion.java:244)
at org.dbunit.Assertion.assertEquals(Assertion.java:204)
at org.dbunit.Assertion.assertEquals(Assertion.java:186)
The reason for such failure is item 2: DbUnit uses the content of the first line to
define how many columns it is expecting thereafter.
Listing 17.14
Test case that demonstrates the missing column issue
[...]
public class NULLTest extends AbstractDbUnitTestCase {
@Test
public void testNULL() throws Exception {
IDataSet okDataset = getDataSet("/user-ok.xml");
DatabaseOperation.CLEAN_INSERT.execute(dbunitConnection, okDataset);
IDataSet actualDataSet = dbunitConnection.createDataSet();
assertEquals(okDataset, actualDataSet);
IDataSet revertedDataSet = getDataSet("/user-reverted.xml");
Assertion.assertEquals(revertedDataSet, actualDataSet);
}
}
There are many ways to resolve this issue. The two most common ones are
Using a ReplacementDataSet
Using a DTD
Let's look at both in detail.
 
 
 
 
Search WWH ::




Custom Search