Java Reference
In-Depth Information
L
OGGING
Earlier on we said that DbUnit would warn us about the missing column in the
XML
issue, but if you run the
testNULL()
method, it fails without any warning. If that hap-
pens, it means DbUnit logging is somehow disabled.
There's no
API
or DbUnit configuration file to explicitly enable logging. Instead,
you must configure
SLF4J
in your project. It isn't the intent of this topic to explain how
SLF4J
works or why the DbUnit chose this tool (until release 2.2, DbUnit used no log-
ging framework at all). Briefly, you need to add to the project's classpath a
JAR
con-
taining a real
SL4J
implementation. In our cases we didn't see any log because the
project's Ant script is explicitly using sl4j-nop.jar, which doesn't log anything (this is
also the default implementation included in your Maven project if you just add
DbUnit as a project dependency).
If your project already uses a logging framework (like log4j or Java's
java.util.
logging
), chances are there's an
SLF4J
provider for that framework, so you can just
include its
JAR
(such as sl4j-log4j12.jar) in the classpath. If you don't use any logging
framework, the easiest solution (the one that requires no extra configuration) is to
add sl4j-simple.jar. This provider sends info messages to
System.out
, sends warnings
and errors to
System.err
, and ignores all other logging levels (like debug and trace).
Adding sl4j-simple.jar to the classpath and running the test case again, we get the
aforementioned warning:
474 [main] WARN org.dbunit.dataset.xml.FlatXmlProducer - Extra columns on
➥
line 2. Those columns will be ignored.
474 [main] WARN org.dbunit.dataset.xml.FlatXmlProducer - Please add the
➥
extra columns to line 1, or use a DTD to make sure the value of those
➥
columns are populated or specify 'columnSensing=true' for your
➥
FlatXmlProducer.
474 [main] WARN org.dbunit.dataset.xml.FlatXmlProducer - See FAQ for more
➥
details.
Whenever you're facing problems that sound like a DbUnit bug or usage issue, try
enabling the lower logging levels
9
like debug or trace. DbUnit will output a lot of
debugging information, which will hopefully help you resolve the issue.
17.6
Creating datasets from existing database data
So far in the examples, we created dataset
XML
files from scratch, in a bottom-up
approach. This is the ideal situation when you're doing pure
TDD
, but often you need
to create these files from the data already in the database.
For instance, you might be working on a big project, where the database develop-
ment is done by a separate team of
DBA
s and a
QA
team maintains a database instance
full of testing data. Typically in these situations, your Java code (and test cases) will
have to deal with complex scenarios, like tables with dozens of columns and many for-
eign key relationships. It would be unpractical and error prone to create the datasets
9
Notice that you'll need a better SL4J implementation than sl4j-simple in this case.