Thursday, July 18, 2013

Test Your Domain Layer With H2 Embeded Database

In most enterprise level applications testing the persistence unit or the dao layer has a significant advantage since it depends on all upon layers. Here I would explain how you can use spring framework support to unit test your persistence unit.

First have a look on a simple domain class. 
@Table(name = "customer", schema = "", catalog = "")
public class Customer {
    private long id;     private String firstName;     private String lastName;     private String address;     @Id     @Column(name = "id", nullable = false, insertable = true, updatable = true)     @GeneratedValue(strategy = GenerationType.AUTO)     public long getId() {         return id;       // remaining getters and setters with mapping annotations

Next we need to have a DAO class for persisting operations. (here I have used generic DAO pattern)


public interface GenericDao{    public E save(E e);
    public E update(E e);
    public void delete(long id);
public abstract class AbstractGenericDao implements GenericDao {

    protected EntityManager entityManager;

    private Class type;

    public AbstractGenericDao() {
        this.type = (Class) ((ParameterizedType) getClass()
    public E save(E e) {
        return e;

    public E update(E e){
        return e;

    public void delete(long id){
        entityManager.remove(entityManager.getReference(type.getClass(), id));

Next we can extend this base class for DAO implementations such that they will inherit the basic CRUD operations.
public interface CustomerDao extends GenericDao {

        // customer specific method declarations goes here 



public class CustomerDaoImpl extends AbstractGenericDao implements CustomerDao {
     // customer specific method implementation goes here
So now we are done with the DAO implementation. Its time unit test this implementation.

For that I am using a spring test application context with in memory h2 data source as follows.

    xmlns:xsi="" xmlns:context=""






Here you need to enable package scanning for requires java packaged for injecting the dao instance you need to test. Only difference with your real application context file is the inmemoey data h2 data base.

Then you need to write a junit class with loading the above test application context.
@ContextConfiguration(locations = {"classpath*:testContext.xml"})
public class CustomerDaoImplTest {
    private CustomerDao customerDao;

    public void test_save_customer(){
        Customer cm = CommonObjects.getCustomerDomainObj();         Customer afterSave =;         Assert.assertNotNull(afterSave);         Assert.assertNotEquals(0, afterSave.getId());     }       // Remaining assertions goes here

No comments:

Post a Comment