title: Mockito vs H2-In Memory to test a project that uses Hibernate
body: <p>In a project that I am working on I have used Hibernate to interact with the database. I am trying to test the methods in the Hibernate class. I came across two things.</p>
  
  <pre><code>1. Mockito
  2. H2-In Memory for testing
  </code></pre>
  
  <p>Here is my class</p>
  
  <p><strong>UserDAOImpl.java</strong></p>
  
  <pre><code>public class UserDAOImpl implements UserDAO {
      public static final Logger LOG = LoggerFactory.getLogger(UserDAOImpl.class);
      private static Session session;
  
  private static void beginSession() {
      session = DbUtils.getSessionFactory().openSession();
      session.beginTransaction();
  }
  
  @Override
  public void addUser(String username, String password) {
      User user = new User(username, password);
      beginSession();
      session.save(user);
      session.getTransaction().commit();
      session.close();
      }
  }
  </code></pre>
  
  <p>To test the above class,  which is a good method. 
  Shall I use mocking to write Junit tests or use an in memory database for testing. 
  I don't want to test it with the database that I use for development because the test cases will corrupt it. </p>