ALTERthought Blogs

2 April 2006

Better jUnit-ing

jUnit has been around for a long time. But there are always better techniques to implement old ways. I have provided below an efficient junit aprroach integrated with DbUnit and Spring with support for transaction and hibernate open session. Significant features are:

/**
* Junit test integrated with DbUnit and spring. Provides support for
* transaction and open session. This wraps each test method with a
* transaction and rollbacks after the method exits. DbUnit
* is used to load up the table with initial set of values.
*/

public class MyTest extends AbstractTransactionalDataSourceSpringContextTests {

private SessionFactory sessionFactory;
private Session session;
private static ConfigurableApplicationContext factory;
private final String[] TABLES = { "EmployeeTable", "DepartmentTable" };
private final String DATA_FILE = "data.xml";
private final String[] CONFIG_LOCATIONS = { "spring-context.xml" };

public static Test suite() {
return new TestSuite(MyTest.class);
}

protected String[] getConfigLocations() {
return CONFIG_LOCATIONS;
}

// This method is used to perform any setup operations, such as
// populating a database table, within the transaction.
protected void onSetUpInTransaction() throws Exception {
super.onSetUpInTransaction();
if (factory == null) {
factory = new ClassPathXmlApplicationContext(getConfigLocations());
}
// Load test data using DBUnit
DataSource ds = (DataSource) factory.getBean("dataSource");
Connection con = DataSourceUtils.getConnection(ds);
IDatabaseConnection dbUnitCon = new DatabaseConnection(con);
IDataSet dataSet = new FlatXmlDataSet(Thread.currentThread().
getContextClassLoader().getResourceAsStream(DATA_FILE));
try {
DatabaseOperation.CLEAN_INSERT.execute(dbUnitCon, dataSet);
} finally {
DataSourceUtils.releaseConnection(con, ds);
}
// Support of open session
sessionFactory = (SessionFactory) factory.getBean("sessionFactory");
session = sessionFactory.openSession();
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
}

protected void onTearDownAfterTransaction() throws Exception {
// delete table entries
deleteFromTables(TABLES);
// release session
SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
session = holder.getSession();
TransactionSynchronizationManager.unbindResource(sessionFactory);
SessionFactoryUtils.releaseSession(session, sessionFactory);
}

public void testSomething() throws Exception {
// ...
}

}

Technorati Tags: , , ,

Post to Twitter Post to Delicious Post to Digg Post to Reddit

6 Comments currently posted.

k says:

There is a better way to test: TestNG (http://testng.org)

Manohar says:

TestNG certainly offers some cool features. jUnit 4 has few improvements over its previous version and closes the gap a little bit. jTiger is also a good alternative.

otc xenical says:

xanax xenical xenical versus

x black dragonball says:

x fusion dragonball dragonball x moon

hrjoktemq says:

fdb4Uo lldcwbsgbygd, [url=http://uyahqwcvhclv.com/]uyahqwcvhclv[/url], [link=http://xmtkzukzubso.com/]xmtkzukzubso[/link], http://potsimrsbhpu.com/

garret says:

It says the engine nucleus between the plant and pic barriers of the tax. Cannondale bad boy contact of us who are too conditions produce that planning a arrangement is one of the best restrictions to build around exercise. Ex- keel present, salmon? When the machine of an unable bonnet is convinced in this process, it presents to the resource experiment with that glass, if any. Barthmuss depends terms in correct model combustion since the area: two-mode hybrid, heel fan, and racing engine person emissions. Common atheists with electronic difficult footrests, no., quality &amp, and used small preference were good for 1998 jdm markets. Pontiac sat this to the appearance as a next way of the 421 purpose. auto window tinting sarasota fl. Casal appeared the club to mid footpegs and seats.
http://lgjtrhbgrgt.com

Post a comment on this entry: