Using TestNG on Spring Framework and Hibernate Test Case

Dear readers, at the time I wrote this article, I just have 2 months of being an employee at the IT Consultant Company in Jakarta, Indonesia. But that’s not what I want to share in this article. In this company, I was expected to follow the java development stack which is applied in this company. The Spring Framework and Hibernate are include.

I’ve been 6 years in Java programming language, but I admit, I never involve Hibernate framework as an ORM(Object Relational Mapping) method on my projects development stack. While Spring Framework, I’ve been using it since 2 years ago. This condition force me to do a little adaptation of this Hibernate Framework.

I often met some difficulties which is stress me so much, when the codes of the dao layer using a hibernate criteria. For a few developers who always using hibernate framework, it should be easier to use hibernate criteria, but for me, it really stress me so much.

To find a method on a dao layer work or not, sometimes it wouldn’t so effective to test it by deploying whole application just only to test one method. And I decide to use unit-test when testing dao layer methods, so I don’t need to deploy whole application.

Okay, below is the step to create a unit-test in order to test the methods on dao layer which created using Spring Framework and hibernate:

  1. Create a hibernate configuration file, Spring Context file, and all those supported files, and put them onto test/resources folder just like the picture below:
  2. Create a file called TestDao, and put it onto test/java folder. The codes of the files is below:
package com.mojo.maven.test;

import com.mojo.maven.dao.NewsCategoryDao;
import com.mojo.maven.dao.NewsDao;
import com.mojo.maven.model.News;
import com.mojo.maven.model.NewsCategory;
import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.hibernate.context.ManagedSessionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

@ContextConfiguration(locations={"classpath*:maven1ApplicationContext-test.xml"})
public class TestDao extends AbstractTestNGSpringContextTests{
    private Logger logger = Logger.getLogger(TestDao.class);

    @Autowired
    public SessionFactory sessionFactory;
    private Session session;

    @Autowired
    private NewsDao newsDao;

    @Autowired
    private NewsCategoryDao newsCategoryDao;

    /**
     * Inisialisasi hibernate context sebelum test
     */
    @BeforeMethod
    void init() {
        logger.debug("init session factory");
        session = sessionFactory.openSession();
        ManagedSessionContext.bind(session);
    }

    /**
     * cleanup hibernate context setelah test
     */
    @AfterMethod
    void destroy() {
        logger.debug("destroy session factory");
        session.close();
        ManagedSessionContext.unbind(sessionFactory);
    }

    @Test
    public void testGetNews(){
        News news = newsDao.getById(1);
        logger.info("News ID : " + news.getId());
        logger.info("News Title : " + news.getNewsTitle());
        logger.info("News URL : " + news.getNewsUrl());
        logger.info("News Category : " + news.getNewsCategory().getCategoryName());
        logger.info("News Category Description : " + news.getNewsCategory().getCategoryDescription());
    }
}

What we have to pay attention on the class are, the class have an annotation @ContextConfiguration and extends AbstractTestNGSpringContextTests class which is provide as a base class to use it as a test class for TestNG Framework. Also on the annotation @ContextConfiguration  defined the location of the Spring Context, so the compiler can find a Spring Context file when the execution is triggered.

The next thing which have to note are 2 methods which is defined on the class, those are void init() method and void destroy() method. void init() method, given an annotation @BeforeMethod which have a function as an hibernate context initializations before all other the test method executed. While void destroy() method given an annotation @AfterMethod which have a function to close a hibernate session after all test method executed.

Well, that’s all that I can share for this time. If you feel the codes need more improvement, so go head, to make it perfect.

I’ll hope it usefull.

Josescalia

———————————————-

Advertisements