title: Testing Hibernate DAO methods with mockito
body: <p>I have visited a lot of blogs and websites to learn how I can test my DAO methods in Hibernate with Mockito, but I haven't found any specific example that could help me with my code. All I have found is that I have to use Integration test instead of JUnit test but I don't really know how to do that with my code.</p>
  
  <p><strong>QUESTION:</strong> How can I test my DAO methods as good as possible?</p>
  
  <p><strong>MY CODE:</strong></p>
  
  <p>My test with only the mockito part:</p>
  
  <pre><code>import org.junit.Before;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.mockito.Mock;
  import org.mockito.MockitoAnnotations;
  import org.mockito.junit.MockitoJUnitRunner;
  
  @RunWith(MockitoJUnitRunner.class)
  public class UserDAOTest
  {
  
  @Mock
  private UserDAO userDAO;
  
  @Before
  public void setUp()
  {
      MockitoAnnotations.initMocks(this);
  }
  
  @Test
  public void testAddUser_AddsNewUser()
  {
  
  }
  
  @Test
  public void testDeleteUser_DeletesUser()
  {
  
  }
  
  
  @Test
  public void testGetUser_FetchUser()
  {
  
  }
  
  @Test
  public void testGetUsers_FetchesAllUsers()
  {
  
  }
  }
  </code></pre>
  
  <p>My UserDAO:</p>
  
  <pre><code>import Hibernate.HibernateUtil;
  import Hibernate.Models.User;
  import org.hibernate.HibernateException;
  import org.springframework.stereotype.Repository;
  import org.hibernate.Session;
  import org.hibernate.Transaction;
  import java.util.List;
  
  //@Transactional
  @Repository
  public class UserDAO extends GeneralDAO
  {
  
  public void addUser(User user)
  {
      add(user);
  }
  
  
  /**
   * Deletes a user from the database based on the userID
   * @param userID
   */
  
  public void deleteUser(int userID)
  {
      User user = new User();
      delete(userID, user);
  }
  
  
  public User getUser(int userID) throws Exception
  {
      Transaction transaction = null;
      User user = null;
      try (Session session = HibernateUtil.getSessionFactory().openSession()) 
  {
          // start a transaction
          transaction = session.beginTransaction();
  
          // Gets the user object
          user = session.get(User.class, userID);
  
          // commit transaction
          transaction.commit();
  
          //closing session
          session.close();
  
          return user;
  
      } catch (Exception e) {
          if (transaction != null) {
              transaction.rollback();
          }
          e.printStackTrace();
          return user;
      }
  }
  
  
  public List&lt;User&gt; getUsers() throws HibernateException, Exception
  {
      try(Session session = HibernateUtil.getSessionFactory().openSession()){
          return session.createQuery("FROM User", User.class).getResultList();
      }
  
  }
  }
  </code></pre>
  
  <p>My GeneralDAO:</p>
  
  <pre><code>import Hibernate.HibernateUtil;
  import org.hibernate.Session;
  import org.hibernate.Transaction;
  import org.springframework.stereotype.Repository;
  
  //@Transactional
  @Repository
  public class GeneralDAO
  {
  public void add(Object obj)
  {
      Transaction transaction = null;
      try (Session session = HibernateUtil.getSessionFactory().openSession())
      {
          // start a transaction
          transaction = session.beginTransaction();
  
          // add the user object
          session.save(obj);
  
          // commit transaction
          transaction.commit();
  
          //closing session
          session.close();
  
  
      } catch (Exception e)
      {
          if (transaction != null)
          {
              transaction.rollback();
          }
          e.printStackTrace();
      }
  }
  
  public void delete(int userID, Object obj)
  {
      Transaction transaction = null;
      try (Session session = HibernateUtil.getSessionFactory().openSession())
      {
  
          obj = session.load(obj.getClass(), userID);
  
          // start a transaction
          transaction = session.beginTransaction();
  
          //deleting the user from the db
          session.delete(obj);
  
          // commit transaction
          transaction.commit();
  
          //closing session
          session.close();
  
  
      } catch (Exception e)
      {
          if (transaction != null)
          {
              transaction.rollback();
          }
          e.printStackTrace();
      }
  }
  }
  </code></pre>