<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Dbunit on despatches</title><link>https://icle.es/tags/dbunit/</link><description>Recent content in Dbunit on despatches</description><generator>Hugo</generator><language>en</language><lastBuildDate>Fri, 20 Jun 2025 09:25:00 +0100</lastBuildDate><atom:link href="https://icle.es/tags/dbunit/index.xml" rel="self" type="application/rss+xml"/><item><title>Hibernate Domain Model Testing</title><link>https://icle.es/2008/12/23/hibernate-domain-model-testing/</link><pubDate>Tue, 23 Dec 2008 22:14:42 +0000</pubDate><guid>https://icle.es/2008/12/23/hibernate-domain-model-testing/</guid><description>&lt;p>One of my pet peeves with Hibernate has always been how difficult it was to test
it. I want to test the persistence of data, loading the data back and any
specific funtionality with the domain model.&lt;/p>
&lt;p>Simple? NO! The main problem was the management of the data set. I had set up,
in the past fairly interesting classes to test the functionality using
reflection, and injecting the data from the classes themselves through the data
provider mechanism of &lt;a href="http://testng.org/d" title="TestNG">TestNG&lt;/a>. However, this was
error prone and clunky at best. It also made dependency management of data quite
cumbersome.&lt;/p>
&lt;p>With a view to resolving this, I also looked at
&lt;a href="http://dbunit.sourceforge.net/" title="DbUnit">DbUnit&lt;/a>,
&lt;a href="http://unitils.org/" title="Unitils">unitils&lt;/a> and
&lt;a href="http://ejb3unit.sourceforge.net/" title="Ejb3Unit">Ejb3Unit&lt;/a>. They all did some
things that I liked but lacked some functionality that was important.&lt;/p></description><content:encoded><![CDATA[<p>One of my pet peeves with Hibernate has always been how difficult it was to test
it. I want to test the persistence of data, loading the data back and any
specific funtionality with the domain model.</p>
<p>Simple? NO! The main problem was the management of the data set. I had set up,
in the past fairly interesting classes to test the functionality using
reflection, and injecting the data from the classes themselves through the data
provider mechanism of <a href="http://testng.org/d" title="TestNG">TestNG</a>. However, this was
error prone and clunky at best. It also made dependency management of data quite
cumbersome.</p>
<p>With a view to resolving this, I also looked at
<a href="http://dbunit.sourceforge.net/" title="DbUnit">DbUnit</a>,
<a href="http://unitils.org/" title="Unitils">unitils</a> and
<a href="http://ejb3unit.sourceforge.net/" title="Ejb3Unit">Ejb3Unit</a>. They all did some
things that I liked but lacked some functionality that was important.</p>
<p>This led me to write a simple testing infrastructure. The goal was
straightforward.</p>
<ul>
<li>I need to be able to define data in a CSV (actually it was seperated by the
pipe character |, so PSV) based on entities.</li>
<li>The framework should automatically persist the data (and fail on errors)</li>
<li>It should test that it can load all that data back</li>
<li>It should run as many automated tests on the DOM as possible.</li>
</ul>
<p>The framework uses the CSV files to read the data for each of the classes (using
the excellent <a href="http://supercsv.sourceforge.net/" title="SuperCsv">SuperCsv</a> library).
It needs an Id field for internal reference. As long as the id&rsquo;s match within
the CSV files for the relationships, it will be persisted correctly into the
database even when the persisted id&rsquo;s are different.</p>
<p>For example, I could have a Contact.csv with 5 records (ids 1 through 5) and a
Company.csv with 3 records (ids 1 through 3).</p>
<p>The Contact.csv records can map to the id specified in the Company.csv file and
when the records get persisted, they will be associated correctly, even if the
id&rsquo;s in the database end up being different.</p>
<p>The framework also looks for the CSV file which has the same name as the class
within the location defined within the configuration file. This means that as
long as the filename matches the class name, the data loading is automatic.</p>
<p>For simple classes, the Test case is as simple as:</p>
```java
public class CompanyTest extends DOMTest<Company> {

public CompanyTest() { super(Company.class); } }
```
<p>The system (with the help of testNG) is also easily flexible to define object
model dependencies. Just override the persist method (which just calls the
super.persist) and define the groups to be persist and <code>&lt;object&gt;.persist</code></p>
<p>in this particular case, it would be</p>
```java
@override
@Test(groups={"persist", "Company.persist"}
public void persist() {
    super.persist();
}
```
<p>For all dependent classes, I then depend on the Company.persist group (For the
ContactTest class for example, since it needs to link to the Company object)</p>
<p>You can specify OneToOne and ManyToOne relationships with just the CSV files -
just defining the field name and the id of the object to pull in.</p>
<p>ManyToMany is more complex and requires an interim object to be created within
the test section. If the Contact to Company relationship above was ManyToMany,
we would create a ContactCompany class with just the two fields - Contact &amp;
Company, then create a csv file with three fields, id, Contact, &amp; Company. The
framework currently always needs an id field.</p>
<p>You would then need to write a method within the ContactTest or CompanyTest(I
use the owning side) to read the CSV file in and pump the data. This process is
a little bit complex just now.</p>
<p>With an appropriate amount of test data, you are able to write a test suite that
can consistently test your domain model. More importantly, you can configure it
to drop the database at the start of each run so that once the tests are
complete, you have a database structure and data than can be used for testing of
higher level components (EJB/Spring/UI/WebApp)</p>
<p>We currently use this framework to test the domain model as well as distribute a
data set for development and testing of the higher tier functionalities.</p>
<p>For the future, there are several additional features this framework needs:</p>
<ul>
<li>It currently needs the setters/getters &amp; constructors to be public. This needs
to be FIXED</li>
<li>Refactor the ManyToMany Relationship code to make it easier and simpler to
test and pump data</li>
<li>See if we can ensure that additional tests which data is done within a
transaction and rolled back so that the database is left in the &ldquo;CSV Imported&rdquo;
state on completion of tests</li>
<li>Easier Dependency management if possible</li>
</ul>
<p>This framework is still inside the walls of Kraya, but once the above issues are
resolved and it is in a releasable state, it will be published into the open
source community. If you are interested in getting a hold of it, email me and
I&rsquo;ll provide you with the latest version.</p>
<p>The easier and quicker it is to test, the more time we can spend on writing
code&hellip; :-) The higher the coverage of the tests, the more confident you can be
of your final product.</p>
<p>To more testing&hellip;</p>]]></content:encoded></item></channel></rss>