• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Testing DAO with Mockito

Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to Mockito and trying to write a test for my demo project. I have a project, using Spring 5 and Hibernate. I have a DAO class which use Hibernate session and query.

public class CustomerDaoImpl implements CustomerDao {

   private SessionFactory sessionFactory;

public Customer getCustomerById(int id) {
Session session = null;
Customer customer = null;

session = sessionFactory.openSession();
customer = session.find(Customer.class, id);
}catch(Exception e) {
throw (e);
}finally {
return customer;

// Test Class
@ContextConfiguration(locations = "classpath:spring-hibernate-config-test.xml")
public class CustomerDaoTest {

private CustomerDaoImpl customerDao;

SessionFactory sessionFactory;

Customer customer;

public void setupMock(){
    sessionFactory = mock(SessionFactory.class);

@DisplayName("DAO: Find customer by given id")
public void testGetCustomerById() {
int customerId = 14;
Customer customer = new Customer(14, "Mike");
when(sessionFactory.openSession().find(Customer.class, customerId)).thenReturn(customer);

    assertEquals(customerId, customer.getIfCustomerId());

at com.primis.dao.CustomerDaoTest.testGetCustomerById(CustomerDaoTest.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)

For some reason my session is null. Please can someone advice where I am going wrong.

Thank you
Saloon Keeper
Posts: 15655
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is your session null or your session factory?
Posts: 22791
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ish Fad wrote:

With Mockito, every method of a mocked object does the following:
  • If the return type is void, it does nothing
  • If the return type is a primitive, it returns the default value for that type (false, 0, etc)
  • If the return type is a collection or map, it returns an empty instance (or null, I can't remember)
  • Otherwise it returns null

  • To override this behaviour, you need to setup the return values.  You try to do that; however, you use a chained call. Since you didn't specify what sessionFactory.openSession() should return, it returns null. If you then call find on it, an NPE is thrown.

    In this case, you'll have to do nested mocking. Create a mock for the Session; either as a field annotated with @Mock, or programmatically using mock(Session.class). Then setup the mocking:
    F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
      Bookmark Topic Watch Topic
    • New Topic