Wednesday, May 28, 2008

Unit Testing Repositories with DbUnit and Spring

In this post, I will create a base class for repository tests. I used DBUnit to populate database configured in a testContext.xml. And I used Spring test annotations to wire test classes with dataSource and repositories.
Requirements
* Spring 2.5
* DbUnit 2.2.2
* Junit 4.4

Here you are

import javax.sql.DataSource;

import org.dbunit.DataSourceDatabaseTester;
import org.dbunit.IDatabaseTester;
import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.IDataSet;
import org.dbunit.operation.DatabaseOperation;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:*testContext.xml" })
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class })

public abstract class AbstractRepositoryTest {

private IDatabaseTester databaseTester;

private DataSource dataSource;


@Autowired
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}

@Before
public void setUpDatabase() throws Exception {
databaseTester = new DataSourceDatabaseTester(dataSource);
databaseTester.setTearDownOperation(DatabaseOperation.DELETE_ALL);
IDataSet dataSet = getDataSet();
databaseTester.setDataSet(dataSet);

databaseTester.onSetup();
}

protected abstract IDataSet getDataSet() throws DataSetException ;

@After
public void tearDownDatabase() throws Exception {

if (databaseTester != null) {
databaseTester.onTearDown();
}
}

}


And here is a saimple example test for MyRepository.

import java.util.List;

import javax.sql.DataSource;

import org.dbunit.DataSourceDatabaseTester;
import org.dbunit.IDatabaseTester;
import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.csv.CsvURLDataSet;
import org.dbunit.operation.DatabaseOperation;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;


public class MyRepositoryTest extends AbstractRepositoryTest {

private MyRepository myRepository;

@Autowired
public void setMyRepository(@Qualifier("myRepository") MyRepository myRepository) {
this.myRepository = myRepository;
}


protected IDataSet getDataSet() throws DataSetException {
// returns CVS files in the currenct directory
return new CsvURLDataSet(getClass().getResource(""));
}

@Test
public void listMyXEntities() {
List xList = myRepository.listXEntities();

Assert.assertEquals(3, xList.size());
}
}


I really like the simplicity that comes with annotations!

3 comments:

Anonymous said...

Hi,

I keep coming to this website[url=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips].[/url]turgay.blogspot.com is filled with quality info. Let me tell you one thing guys, some time we really forget to pay attention towards our health. Here is a fact for you. Recent Research indicates that about 50% of all U.S. adults are either chubby or weighty[url=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips].[/url] Therefore if you're one of these people, you're not alone. Its true that we all can't be like Brad Pitt, Angelina Jolie, Megan Fox, and have sexy and perfect six pack abs. Now next question is how you can achive quick weight loss? [url=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips]Quick weight loss[/url] is not like piece of cake. If you improve some of your daily diet habbits then, its like piece of cake to quickly lose weight.

About me: I am webmaster of [url=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips]Quick weight loss tips[/url]. I am also health expert who can help you lose weight quickly. If you do not want to go under difficult training program than you may also try [url=http://www.weightrapidloss.com/acai-berry-for-quick-weight-loss]Acai Berry[/url] or [url=http://www.weightrapidloss.com/colon-cleanse-for-weight-loss]Colon Cleansing[/url] for quick weight loss.

Anonymous said...

Wow neat! This is a really great site! I am wondering if anyone else has come across something
exactly the same in the past? Keep up the great work!

Anonymous said...

Its my chief things to post on this forum,just wannat make some friends here.if its not allowed to post on this enter,gladden strike out this thread.Nice to adjoin you!

---------------------------------------------------------------
[url=http://www.sexybags.info/rssrock.html]My designer handabgs[/url]