Testing Spring beans against database with DbUnit

April 5 2009one Commented

Categorized Under: Agile, Engroup, Java, Software Engineer, Technology, Testing

This is another post of testing series we wrote so far and it discusses how to write testing against database. We prefer writing unit test for whole kind of our services, it includes the testing against POJO, LDAP, database, content repository, email. Here the topics relate to testing in our site:

Introduction
There are some techniques to write testing application services interact with database. There are:

Using Mock Object

Mock object are simulated objects that mimic the behavior of real objects in controlled ways. Using the mock object could be increase the performance of unit test, reduce the cost of maintenance also reduce the complexity of setting the test objects. In java world, easymock (http://easymock.org/) is the good mock library to help developers write mock easily.

Using Development Database

Testing runs against the development database. It has separated testing database per each workspace. It takes more time in setting testing environment, more time to execution, but it is useful in case we need to write the testing for database test script (not focus on business on service layer).
Database testing with DbUnit

DbUnit (http://www.dbunit.org/) is a JUnit extension targeted at database-driven projects that, among other things, puts your database into a known state between test runs. You should use dbUnit to write unit/integration testing on service layer. However, dbUnit could not be use in:

  • Performance testing: development performance could say nothing about the performance of database.
  • Stress/Volume testing: you could not write millions of database records using xml format.

With the completed database schema which has many foreign keys, it may takes you much time to maintain the known state of database, so one practice is omitting all foreign keys in testing database. It will reduce much time of maintaining database test script.

Integrate DbUnit and Spring Testing

Spring provides the excellent testing framework since 2.5 supports java annotation (http://static.springframework.org/spring/docs/2.5.x/reference/testing.html). To configure the Test Context, Spring use the test runner is SpringJUnit4ClassRunner. So in order to integrate DbUnit with Spring Testing framework, we extended this runner by our own class EngroupClassRunner and our testing has format:

@RunWith(EngroupClassRunner.class)
@ContextConfiguration(locations = { "/spring-service-test.xml" })
public class ExtendedTest{
    ...
    @DataSet
    @Test
    public void testSave() {
        ...
    }
}

With EngroupClassRunner, it would use the database script locates in the same location in testing class (for example: the database script would be located at com/engroup/module/hr/service/EducationTest.xml if the test class has the full name com.engroup.module.hr.service.EducationTest).

Override the database source to database testing configuration

Replace the spring configuration file by another spring configuration file in testing. For example: replace the code

<osgi:reference id="dataSource" interface="javax.sql.DataSource" />
<bean id="hrSqlMapClient"
    class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    <property name="configLocation"
        value="classpath:hr-sqlmap-config.xml" />
    <property name="dataSource" ref="dataSource" />
</bean>

By

<bean id="dataSource"
  class="com.engroup.test.DataSourceFactoryBean" />
<bean id="commonSqlMapClient"
        class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    <property name="configLocation"
        value="classpath:common-sqlmap-config.xml" />
    <property name="dataSource" ref="dataSource" />
</bean>

with com.engroup.test.DataSourceFactoryBean is the factory bean creates the data source from testing configuration and points to testing database.

and you write the database script in xml format as usual dbUnit format:

<!DOCTYPE dataset SYSTEM "../../../../../../engroup.dtd" >
<dataset>
    <m_hr_education id="1" course="Java" durationinmonth="10" institute="USA"/>
    <m_hr_education id="2" course="C#" durationinmonth="5" institute="VN"/>
</dataset>

Finally, you run the test to see whether your service is ok.

I include the some core classes of Engroup testing, not the whole package could be testable (because if I do so, you must have the testing database at your side). Here is the attachment.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • BarraPunto
  • Bitacoras.com
  • blinkbits
  • BlinkList
  • blogmarks
  • BlogMemes
  • BlogMemes Cn
  • BlogMemes Fr
  • BlogMemes Jp
  • BlogMemes Sp
  • Blogosphere News
  • Blogsvine
  • blogtercimlap
  • Book.mark.hu
  • Bumpzee
  • co.mments
  • connotea
  • De.lirio.us
  • Design Float
  • DotNetKicks
  • DZone
  • eKudos
  • email
  • Fark
  • Faves
  • feedmelinks
  • Fleck
  • Furl
  • GeenRedactie
  • Global Grind
  • Gwar
  • Haohao
  • HealthRanker
  • Hemidemi
  • Identi.ca
  • IndianPad
  • Internetmedia
  • kick.ie
  • Kirtsy
  • laaik.it
  • Leonaut
  • LinkaGoGo
  • LinkArena
  • LinkedIn
  • Linkter
  • Live
  • Ma.gnolia
  • Meneame
  • MisterWong
  • MisterWong.DE
  • muti
  • MyShare
  • MySpace
  • N4G
  • Netvibes
  • Netvouz
  • NewsVine
  • NuJIJ
  • Ping.fm
  • PlugIM
  • Pownce
  • ppnow
  • Print
  • Propeller
  • Ratimarks
  • Rec6
  • Reddit
  • SalesMarks
  • Scoopeo
  • scuttle
  • Segnalo
  • Shadows
  • Simpy
  • Slashdot
  • Smarking
  • Socialogs
  • SphereIt
  • Spurl
  • StumbleUpon
  • Symbaloo
  • Taggly
  • TailRank
  • Technorati
  • ThisNext
  • Tipd
  • Tumblr
  • TwitThis
  • Upnews
  • Webnews.de
  • Webride
  • Wikio
  • Wikio FR
  • Wikio IT
  • Wists
  • Wykop
  • Xerpi
  • Yahoo! Buzz
  • YahooMyWeb
  • Yigg

Related posts:

  1. Integration of Jackrabbit OCM and Spring (updated version) In the previous post Jackrabbit OCM and Spring, I have...
  2. How to invoke Spring beans in other osgi bundle Spring Dynamic Modules helps integrating Spring beans to Osgi platform...
  3. Develop Restful application with RESTeasy Web service is complicated, to understand web service you must...
  4. Unit test with Spring Dynamic Modules Follow our story about developing services by using Spring Dynamic...
  5. Some questions and answers about unit testing I have had an interesting discussion with some project managers...

One Response to “Testing Spring beans against database with DbUnit”

  1. mark says:

    Great site, Good info

Leave a Reply