Tag Archives: Mockito

Mock HibernateTemplate with Mockito

Introduction

Mockito is a useful Java library to create mock Objects for Java Unit Testing.
http://site.mockito.org/

I used the framework Mockito in order to test objects difficult to test otherwise such as Struts method, Servlet, Hibernate , etc…

HibernateTemplate object to mock

This is the legacy code i wanted to test with Mockito. HibernateTemplate is a deprecated class but it is still used in this code.

Package com.dao.myclass;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import com.dao.ImyDAO;
import com.dao.exception.MyException;
import com.dao.myEntity;
import com.util.Logger;

public class MyHibernateImpl extends HibernateDaoSupport implements ImyDAO {

    @Override
    public myEntity getMyEntity(String myNumber) throws MyException {
        try {
            if (myNumber == null) {
                Logger.getLogger(MyHibernateImpl.class)
                        .error("myNumber : " + myNumber);
                return null;
            }
            if (myNumber != null && myNumber.length() > 1) {
               process(MyNumber);
            }
            myEntity thismyentity = (myEntity) getHibernateTemplate().get("com.dao.myEntity", myNumber);
            return thismyentity;
        } catch (final DataAccessException e) {
            throw new MyException("error.CodeMyEntity", e);
        }
    }

}

The line of code i had to difficulty to test without Mockito :

myEntity thismyentity = (myEntity) getHibernateTemplate().get("com.dao.myEntity", myNumber);

The code for myEntity :

@Entity
@Table (name="MY_TABLE")
public class myEntity implements Serializable {

    private static final long serialVersionUID = 5447862515324364L;

    @Id
    private String myNumber;

    @Column (name="MYCOLUMN")
    private String myColumn;

    public String getmyNumber() {
        return myNumber;
    }
    public void setmyNumber(String myNumber) {
        this.myNumber = myNumber;
    }
    public String getmyColumn() {
        return myColumn;
    }
    public void setmyColumn(String myColumn) {
        this.myColumn = myColumn;
    }
}

Mock the object HibernateTemplate

I need to mock the object HibernateTemplate in order to test this function. Here is my test class with Junit :

public class TestMyHibernateImpl {

    @Mock
    HibernateTemplate mockTemplate;

I used the annotation @Mock for the creation of a mock object of HibernateTemplate. To use @Mock it is necessary to initialise MockitoAnnotations :

    @Before
    public void initMocks() {
        MockitoAnnotations.initMocks(this);
    }

This is how I test the function getMyEntity:

    @Test
    public void testCasEntityReturned() throws MyException {

        myEntity entity = new myEntity();
        String myNumber = "S54564121SD”;
        String myColumnValue = "myColumn";
        entity.setmyNumber(myNumber);
        entity.setmyColumn(myColumnValue);

        (Mockito.doReturn(entity).when(mockTemplate)).get("com.dao.myEntity", myNumber);
<b>// I tell Mockito to return an entity when this code is used later in getMyEntity(..)</b>

        MyHibernateImpl myColumn = new MyHibernateImpl();
        myColumn.setHibernateTemplate(mockTemplate);
        
        myEntity thismyentity = myColumn.getMyEntity(myNumber);
        Assert.assertNotNull(thismyentity);
        Assert.assertEquals(myNumber, thismyentity.getmyNumber());
        Assert.assertEquals(myColumnValue, thismyentity.getmyColumn());

    }
Advertisements