Home » Java » Enterprise Java » Testing Spring & Hibernate Without XML

About Konrad Garus

Konrad Garus

Testing Spring & Hibernate Without XML

I’m very keen on the improvements in Spring 3 that eventually let you move away from XML into plain Java configuration with proper support from IDE and compiler. It doesn’t change the fact that Spring is a huge suite and it sometimes finding the thing you need can take a while.

XML-free unit tests around Hibernate are one such thing. I knew it was possible, but it took me more than 5 minutes to find all the pieces, so here I am writing it down.

I am going to initialize all my beans in a @Configuration class like this:

public class TestRepositoryConfig {
	public DataSource dataSource() {
		return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)

	public LocalSessionFactoryBean sessionFactoryBean() {
		LocalSessionFactoryBean result = new LocalSessionFactoryBean();
		result.setPackagesToScan(new String[] { "pl.squirrel.testnoxml.entity" });

		Properties properties = new Properties();
		properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
		return result;

	public SessionFactory sessionFactory() {
		return sessionFactoryBean().getObject();

	public HibernateTransactionManager transactionManager() {
		HibernateTransactionManager man = new HibernateTransactionManager();
		return man;

	public OrderRepository orderRepo() {
		return new OrderRepository();

… and my test can look like this:

@TransactionConfiguration(defaultRollback = true)
@ContextConfiguration(classes = { TestRepositoryConfig.class })
public class OrderRepositoryTest {
	private OrderRepository repo;

	private SessionFactory sessionFactory;

	public void testPersistOrderWithItems() {
		Session s = sessionFactory.getCurrentSession();

		Product chestnut = new Product("Chestnut", "2.50");
		Product hazelnut = new Product("Hazelnut", "5.59");

		Order order = new Order();
		order.addLine(chestnut, 20);
		order.addLine(hazelnut, 150);


		Order persistent = (Order) s.createCriteria(Order.class).uniqueResult();
		Assert.assertNotSame(0, persistent.getId());
		Assert.assertEquals(new OrderLine(chestnut, 20), persistent
		Assert.assertEquals(new OrderLine(hazelnut, 150), persistent

There are a few details worth noting here, though:

  1. I marked the test @Transactional, so that I can access Session directly. In this scenario, @EnableTransactionManagement on @Configuration seems to have no effect as the test is wrapped in transaction anyway.
  2. If the test is not marked as @Transactional (sensible when it only uses @Transactional components), the transaction seems to always be committed regardless of @TransactionConfiguration settings.
  3. If the test is marked as @Transactional, @TransactionConfiguration seems to be applied by default. Even if it’s omitted the transaction will be rolled back at the end of the test, and if you want it committed you need @TransactionConfiguration(defaultRollback=false).
  4. This probably goes without saying, but the @Configuration for tests is probably different from production. Here it uses embedded H2 database, for real application I would use a test database on the same engine as production.

That’s it, just those two Java classes. No XML or twisted depedencies. Take a look at my github repository for complete code.

Reference: Testing Spring & Hibernate Without XML from our JCG partner Konrad Garus at the Squirrel’s blog.

(0 rating, 0 votes)
You need to be a registered member to rate this.
1 Comment Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
Email address:

Leave a Reply

1 Comment on "Testing Spring & Hibernate Without XML"

newest oldest most voted
Notify of

If you’re getting an error on this line:

result.setPackagesToScan(new String[] { “pl.squirrel.testnoxml.entity” });

It’s because you’re using Hibernate 3, and that method does not exist, as it was added in Hibernate4. You can get around this by either changing to Hibernate 4, or changing LocalSessionFactoryBean to AnnotatedSessionFactoryBean, with the method: result.setAnnotatedClasses(new Class[]{YOURCLASSNAME.class});

Great guide, helped me out a lot!