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. 
@Entity
@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 {

    @PersistenceContext
    protected EntityManager entityManager;

    private Class type;

    @SuppressWarnings("unchecked")
    public AbstractGenericDao() {
        this.type = (Class) ((ParameterizedType) getClass()
                .getGenericSuperclass()).getActualTypeArguments()[0];
    }
    @Override
    public E save(E e) {
        entityManager.persist(e);
        return e;
    }

    @Override
    public E update(E e){
        entityManager.merge(e);
        return e;
    }

    @Override
    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 

}

@Repository("customerDao")

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="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">


   
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       
       
       
       
   

   
       
       
       
           
               
               
               
           

       

   

   
   
       
   




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.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:testContext.xml"})
@TransactionConfiguration
@Transactional
public class CustomerDaoImplTest {
    @Autowired
    private CustomerDao customerDao;

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

No comments:

Post a Comment