Showing posts with label Unit Test. Show all posts
Showing posts with label Unit Test. Show all posts

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!