title: Mockito testing DAO in createCriteria get NullPointerException
body: <p>i am trying write test for dao layer. I work from hibernate, and when i trying testing my method <code>getAll</code> i get exception <code>NullPointerException</code> in row from <code>createCriteria</code> and i don't know how solve problem.</p>
  
  <p>This is my exception:</p>
  
  <pre><code>java.lang.NullPointerException
      at dao.UserDaoTest.testFindAll(UserDaoTest.java:69)
  </code></pre>
  
  <p>This is my testing class:</p>
  
  <pre><code>@Mock
  private SessionFactory sessionFactory;
  @Mock
  private Session session;
  @Mock
  private Transaction transaction;
  @InjectMocks
  private UserDaoImpl userDao;
  
  private User user;
  
  @Before
  public void setUpUser(){
      user = new User();
      user.setName("is name");
      when(sessionFactory.openSession()).thenReturn(session);
      when(session.beginTransaction()).thenReturn(transaction);
  }
  @Test
  public void testFindAll(){
      List&lt;User&gt; userList = new ArrayList&lt;&gt;();
      userList.add(user);
      when(session.createCriteria(User.class).list()).thenReturn(userList); //NullPointerException
      List&lt;User&gt; userList1 = userDao.findAll();
      assertThat(userList1, is(userList));
  }
  </code></pre>
  
  <p>This is Dao class:</p>
  
  <pre><code>private Session session;
  private Transaction transaction;
  public List&lt;User&gt; findAll() {
      List&lt;User&gt; userList = null;
      session = sessionFactory.openSession();
      transaction = null;
      try{
          transaction = session.beginTransaction();
          userList = session.createCriteria(User.class).list();
          transaction.commit();
      }catch (HibernateException e) {
          if (transaction!=null) {
              transaction.rollback();
          }
          e.printStackTrace();
      }finally {
          session.close();
      }
      return userList;
  }
  </code></pre>