Create and Unit test an Util class

In my application I need to make advanced search against an equipements database, so i created this function in my service interface:

public List<Equipement> findByCriterias(SearchEquipement searchEquipement) ;

I thought that the best way to implement this function is, since I'm working with Spring MVC and Hibernate, to add a function to my DAO interface:

public List<Equipement> getByCriteria(org.hibernate.Criteria criteria) ;

in order to accomplish her mission the findByCriterias method needs to transform the searchEquipement to a org.hibernate.Criteria, so i decided to create an util class to do this (not complete yet) :

public class ApplicationUtil {

private ClassPathXmlApplicationContext context = new
    ClassPathXmlApplicationContext("mmapp/util/application-context-util.xml") ;

private SessionFactory sessionFactory ;

public void setSessionFactory(){
    sessionFactory = (SessionFactory) context.getBean("sessionFactory") ;
}

public synchronized Criteria changeSearchEquipementToCriteria(SearchEquipementsearchEquipement) {
    setSessionFactory() ;
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Equipement.class) ;
    return criteria ;
}
}

the problem is when i tried to unit test this part, i have many troubles.

public class ApplicationUtilUnitTests {

    @Test
    public void testChangeSearchEquipementToCriteria(){
        ApplicationUtil util = new ApplicationUtil() ;
        Criteria criteria = util.changeSearchEquipementToCriteria(null) ;
        assertNotNull(criteria) ;
    }
}

that's after several other tries, now i get this error: org.hibernate.HibernateException: No Session found for current thread

Answers


I believe your answer lies in this post. You're mixing Integration Testing with Unit Testing. By nature of your requirement what you can do is:

  • Use a mocking API such Mockito, PowerMock, EasyMock, or any one that suits you
  • Mock Hibernate's fundamental features such as SessionFactory, Session, and Criteria (e.g. this post)
  • Now, you can perform Unit Testing to verify your method is working properly

Try this...

package com.om39a.spring.training.bean;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;

@RunWith(PowerMockRunner.class)
@PrepareForTest(ApplicationUtil.class)
public class ApplicationUtilTest {

    private ApplicationUtil applicationUtil;

    @Mock
    SessionFactory mockSessionFactory;

    @Mock
    Session mockSession;

    @Before
    public void init() {
        MockitoAnnotations.initMocks(this);
        applicationUtil = PowerMockito.spy(new ApplicationUtil());
    }

    @Test
    public void testApplicationUtil() throws Exception {
        Whitebox.setInternalState("sessionFactory", mockSession);
        PowerMockito.doNothing().when(applicationUtil, "setSessionFactory");
        Mockito.when(mockSessionFactory.getCurrentSession()).thenReturn(
                mockSession);
        applicationUtil.changeSearchEquipementToCriteria(null);
        /* ...
         * ...
         * ...
         * Assert statments goes here 
         * ...
         * ...
         * ...
         */
    }
}

Need Your Help

How to put a border around a google map

google-maps-api-3

How to put a border around a google map? I have used: