title: Mockito testing service layer
body: <p>I'm new to Mockito and trying to test my service layer. My DAO layer is @Autowired in service and Hibernate is also autowired. Hibernate is not loading while testing. I always get NullPointerException. Here is my code:</p>
  
  <p><strong>EmployeeService (Interface)</strong></p>
  
  <pre><code>package com.spring.crud.service;
  
  import java.util.List;
  
  import com.spring.crud.entity.Employee;
  
  public interface EmployeeService {
  
      Employee save(Employee employee);
  
      boolean update(Employee employee);
  
      Employee find(Integer id);
  
      List&lt;Employee&gt; getEmployees();
  
      boolean remove(Integer id);
  }
  </code></pre>
  
  <p>EmployeeServiceImpl (Class)</p>
  
  <pre><code>package com.spring.crud.service;
  
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  
  import com.spring.crud.dao.EmployeeDAO;
  import com.spring.crud.entity.Employee;
  
  @Service
  public class EmployeeServiceImpl implements EmployeeService {
  
  @Autowired
  private EmployeeDAO dao;
  
  public Employee save(Employee employee) {
  
      return dao.save(employee);
  }
  
  public boolean update(Employee employee) {
  
      return dao.update(employee);
  }
  
  public Employee find(Integer id) {
  
      return dao.find(id);
  }
  
  public List&lt;Employee&gt; getEmployees() {
  
      return dao.getEmployees();
  }
  
  public boolean remove(Integer id) {
  
      return dao.remove(id);
  }
  }
  </code></pre>
  
  <p><strong>EmployeeDAO</strong> (Interface)</p>
  
  <pre><code>package com.spring.crud.dao;
  
  import java.util.List;
  
  import com.spring.crud.entity.Employee;
  
  public interface EmployeeDAO {
  
  Employee save(Employee employee);
  
  boolean update(Employee employee);
  
  Employee find(Integer id);
  
  List&lt;Employee&gt; getEmployees();
  
  boolean remove(Integer id);
  }
  </code></pre>
  
  <p><strong>EmployeeDAOImpl</strong> (Class)</p>
  
  <pre><code>package com.spring.crud.dao;
  
  import java.util.List;
  
  import javax.transaction.Transactional;
  
  import org.hibernate.Query;
  import org.hibernate.Session;
  import org.springframework.stereotype.Repository;
  
  import com.spring.crud.entity.Employee;
  
  @Transactional
  @Repository
  public class EmployeeDAOImpl extends HibernateUtil implements EmployeeDAO{
  
  public Employee save(Employee employee) {
  
      Session session = getCurrentSession();
  
      session.save(employee);
  
      return employee;
  }
  
  public boolean update(Employee employee) {
  
      Session session = getCurrentSession();
  
      session.update(employee);
  
      return false;
  }
  
  public Employee find(Integer id) {
  
      Session session = getCurrentSession();
  
      Employee employee = (Employee)session.get(Employee.class, id);  
  
      return employee;
  }
  
  public List&lt;Employee&gt; getEmployees() {
  
      Session session = getCurrentSession();
  
      Query query = session.createQuery("from Employee");
  
      @SuppressWarnings("unchecked")
      List&lt;Employee&gt; employees = (List&lt;Employee&gt;)query.list();
  
      return employees;
  }
  
  public boolean remove(Integer id) {
  
      Session session = getCurrentSession();
  
      Employee employee = (Employee)session.get(Employee.class, id);  
  
      if(employee!=null){
  
          session.delete(employee);
  
          return true;
      }
  
      return false;
  }
  }
  </code></pre>
  
  <p><strong>HibernateUtil</strong></p>
  
  <pre><code>package com.spring.crud.dao;
  
  import javax.annotation.PostConstruct;
  
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
  
  public class HibernateUtil extends HibernateDaoSupport{
  
  @Autowired
  private SessionFactory sessionFactory;
  
  @PostConstruct
  public void init() {
      setSessionFactory(sessionFactory);
  }   
  
  public Session getCurrentSession() {
      return sessionFactory.getCurrentSession();
  }
  }
  </code></pre>
  
  <p><strong>EmployeeServiceTest</strong> (Test class)</p>
  
  <pre><code>package com.spring.crud.test;
  
  import static org.junit.Assert.assertNotNull;
  import static org.junit.Assert.assertNull;
  import static org.junit.Assert.assertTrue;
  import static org.mockito.Matchers.any;
  import static org.mockito.Mockito.mock;
  import static org.mockito.Mockito.when;
  
  import java.util.ArrayList;
  import java.util.Date;
  import java.util.List;
  
  import org.junit.Before;
  import org.junit.Test;
  import org.mockito.MockitoAnnotations;
  import org.mockito.Spy;
  import org.mockito.invocation.InvocationOnMock;
  import org.mockito.stubbing.Answer;
  import org.springframework.test.context.ContextConfiguration;
  
  import com.spring.crud.config.WebConfig;
  import com.spring.crud.dao.EmployeeDAO;
  import com.spring.crud.dao.EmployeeDAOImpl;
  import com.spring.crud.entity.Employee;
  import com.spring.crud.service.EmployeeService;
  import com.spring.crud.service.EmployeeServiceImpl;
  
  @ContextConfiguration(classes = {WebConfig.class})
  public class EmployeeServiceTest {
  
  private EmployeeDAO employeeDAO;
  
  private EmployeeService employeeService = new EmployeeServiceImpl();
  
  @Spy
  List&lt;Employee&gt; employees = new ArrayList&lt;Employee&gt;();
  
  @Before
  public void setup() {
      MockitoAnnotations.initMocks(this);
  
      employeeDAO = mock(EmployeeDAOImpl.class);
  }
  
  @Test
  public void listEmployees() {
  
  }
  
  @Test
  public void create() {
  
      Employee employee = new Employee();
  
      employee.setDateOfBirth(new Date());
      employee.setGender("male");
      employee.setName("Ashutosh");
  
      when(employeeDAO.save(any(Employee.class)))
          .thenAnswer(new Answer&lt;Employee&gt;() {
  
              public Employee answer(InvocationOnMock invocation) throws Throwable {
  
                  Employee employee = (Employee) invocation.getArguments()[0];
  
                  employee.setId(1);
  
                  return employee;
              }
  
          });
  
      assertNull(employee.getId());
  
      employee = employeeService.save(employee);
  
      assertNotNull(employee.getId());
  
      assertTrue(employee.getId()&gt;0);
  }
  
  @Test
  public void edit() {
  
  }
  
  @Test
  public void update() {
  
  }
  
  @Test
  public void remove() {
  
  }
  
  }
  </code></pre>
  
  <p>I can't find much on this on the internet.</p>