Information Technology Reference
In-Depth Information
base. The latter choice presents a new series of challenges—how do
you control the database during testing? Even better, how do you make
those tests repeatable?
By far, the easiest way to make your testing cake and eat it is to
use a database-seeding framework like any of the xDbUnits (such as
NDbUnit for .NET, DbUnit for Java, and PDbSeed for Python). These
frameworks abstract a database's data set into XML files and then
offer the developer fine-grained control as to how this data is seeded
into a database during testing. For example, the snippet shown in Listing
6-18 is from a DbUnit XML seed file.
LISTING 6-18
Sample DbUnit Data File
<word WORD_ID="1" SPELLING="pugnacious" PART_OF_SPEECH="Adjective"/>
<definition DEFINITION_ID="10"
DEFINITION="Combative in nature; belligerent."
WORD_ID="1"
EXAMPLE_SENTENCE="The pugnacious youth had no friends left to pick on."/>
<synonym SYNONYM_ID="20" WORD_ID="1" SPELLING="belligerent"/>
<synonym SYNONYM_ID="21" WORD_ID="1" SPELLING="aggressive"/>
Via DbUnit's DatabaseTestCase , the data in the XML file is
manipulated via operations such as insert, update, and delete. The specific
database is configured by implementing the abstract getConnection
method, and the XML file is located via the getDataSet method (see
Listing 6-19).
Sample Database Test Case
LISTING 6-19
public class DefaultWordDAOImplTest extends DatabaseTestCase {
protected IDataSet getDataSet() throws Exception {
return new FlatXmlDataSet(
new File("test/conf/words-seed.xml"));
}
protected IDatabaseConnection getConnection() throws Exception {
final Class driverClass =
Class.forName("org.gjt.mm.mysql.Driver");
final Connection jdbcConnection =
DriverManager.getConnection(
"jdbc:mysql://localhost/words",
"words", "words");
return new DatabaseConnection(jdbcConnection);
}
Search WWH ::




Custom Search