Suggested Pages

Tuesday, May 15, 2012

Unit Test with Maven, Spring and HSQLDB - Tutorial

In my programming experience, I have learnt the importance of making unit tests on persistence functionalities. In this post I'll show a way to test persistence functionalities with Maven, Spring and HSQLDB.


When you develop a software that has to store data, you usually prepare a testing local database. With HSQLDB used in memory mode you can test persistence unit of work in your local machine in easy way. The database is not persistent and exists entirely in random access memory.

Maven - Surefire Plugin

Maven has a lifecycle composed by different phases. During the test phase maven use the the Surefire Plugin to execute the unit tests of an application. You should put your testing code at src/main/test. Then calling mvn clean test you can execute your tests.

Spring - SpringJUnit4ClassRunner

Spring has a package org.springframework.test.context that provides annotation-driven unit and integration test support. It also provides a custom runner class called SpringJUnit4ClassRunner that permit to run your test.

Now Suppose you have a DAO that has only two methods: saveContact and retrieveContact.
package com.simonefolinojavablog.persistence.dao;

import com.simonefolinojavablog.persistence.entity.Contact;

public interface ContactDAO {

 public void saveContact(Contact contact);
 public Contact retrieveContact(int idContact);

package com.simonefolinojavablog.persistence.dao;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.simonefolinojavablog.persistence.entity.Contact;

@Repository(value = "ContactDAO")
public class ContactDAOImpl implements ContactDAO {

 @PersistenceContext(unitName = "persistenceUnitTest")
 private EntityManager entityManager;

 @Transactional(propagation = Propagation.REQUIRED)
 public void saveContact(Contact contact) {

 @Transactional(propagation = Propagation.REQUIRED)
 public Contact retrieveContact(int idContact) {
  return entityManager.find(Contact.class, idContact);

package com.simonefolinojavablog.persistence.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

public class Contact {

 private int id;
 private String number;

 public int getId() {
  return id;

 public void setId(int id) { = id;

 public String getNumber() {
  return number;

 public void setNumber(String number) {
  this.number = number;

 public String toString() {
  return "Contact [id=" + id + ", number=" + number + "]";

ContactDAOImpl implements simply the storing and the retrieving of Contact class. Both ContactDAOImpl and ContactDAO are located at src/main/java in Maven's structure.
If you are interested in testing these two functionalities, you can create ContactDAOTest class at src/main/test in Maven's structure, as shown below.
package com.simonefolinojavablog.persistence.test;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.simonefolinojavablog.persistence.dao.ContactDAO;
import com.simonefolinojavablog.persistence.entity.Contact;

@ContextConfiguration(value = "ContactDAOTest-context.xml")
public class ContactDAOTest {

 private ContactDAO contactDAO;

 private static Contact contact;
 public static void initContact(){
  contact=new Contact();

 public void testSaveContact() {
  System.out.println("------------testSaveContact(): start -------");
  System.out.println("------------testSaveContact(): end -------");
 public void testRetrieveContact() {
  System.out.println("------------testRetrieveContact(): start -------");
  Contact retrieveContact = contactDAO.retrieveContact(contact.getId());
  System.out.println("------------testRetrieveContact(): end -------");

As you can see ContactDAOTest uses two annotations at class level:
  • @RunWith: tells the runner class that JUnit will invoke instead of the runner built into JUnit;
  • @ContextConfiguration: tells the path where is located application context to load for this test;
Annotations as @Test, @BeforeClass are the same annotations you use in JUnit.

The file shown below is ContactDAOTest-context that represents the application context loaded by the class ContactDAOTest.
In this file, four important beans are declared:
  • dataSource: the datasource c3p0 used to connect to HSQLDB (in memory mode);
  • transactionManager: org.springframework.orm.jpa.JpaTransactionManager;
  • entityManagerFactory: org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean. It has an attribute depends-on="schemaGenerator" which it means that entityManagerFactory depends on the bean schemaGenerator. This bean is called by the container before initializing entityManagerFactory ;
  • schemaGenerator: a bean that creates the schema where entity beans will be stored.


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns=""
 xmlns:aop="" xmlns:context=""
 xmlns:jee="" xmlns:tx=""
 xmlns:xsi="" xmlns:jaxrs=""

 <!-- annotation support -->
 <context:annotation-config />

 <!-- support for transaction -->
 <tx:annotation-driven />

 <!-- scan package for @Repository annotations -->
 <context:component-scan base-package="com.simonefolinojavablog.persistence.dao">
  <context:include-filter type="annotation"
   expression="org.springframework.stereotype.Repository" />

 <!-- HSQLDB datasource -->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  <property name="driverClass" value="org.hsqldb.jdbcDriver" />
  <property name="jdbcUrl" value="jdbc:hsqldb:mem:ex" />
  <property name="user" value="sa" />
  <property name="password" value="" />
 <bean id="entityManagerFactory" depends-on="schemaGenerator"
  <property name="persistenceUnitName" value="persistenceUnitTest" />
  <property name="dataSource" ref="dataSource" />
  <property name="jpaVendorAdapter">
   <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="true" />
    <property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
    <property name="generateDdl" value="true" />

 <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory" ref="entityManagerFactory" />
  <property name="dataSource" ref="dataSource" />

 <bean id="schemaGenerator" class="com.simonefolinojavablog.persistence.test.SchemaGenerator">
  <property name="schema" value="ExampleSchema" />
  <property name="dataSource" ref="dataSource" />

package com.simonefolinojavablog.persistence.test;

import javax.sql.DataSource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.core.JdbcTemplate;

public class SchemaGenerator implements InitializingBean {

    private String schema;
    private DataSource dataSource;

    public String getSchema() {
        return schema;

    public void setSchema(String schema) {
        this.schema = schema;

    public DataSource getDataSource() {
        return dataSource;

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

     public void afterPropertiesSet() throws Exception {
     JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        jdbcTemplate.execute("CREATE SCHEMA " + schema + " AUTHORIZATION DBA");
Finally you have to declare the dependencies in your pom.xml in order to use the features described above. In this snippet i'll show the main dependencies you need. It's important to notice that hsqldb and c3p0 have test scope because they are only used for test aims.


What you'll see from the console is the following output:
 T E S T S
Running com.simonefolinojavablog.persistence.test.ContactDAOTest
------------testSaveContact(): start -------
        (number, id) 
        (?, ?)
------------testSaveContact(): end -------
------------testRetrieveContact(): start -------
    select as id0_0_,
        contact0_.number as number0_0_ 
        ExampleSchema.Contacts contact0_ 
Contact [id=1, number=22xxxx]
------------testRetrieveContact(): end -------
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.596 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

No comments :

Post a Comment

Suggested Pages