title: Hibernate and JUnit
body: <p>I'm new to JUnit testing.Whenever I tried to test a method which have a hibernate query results null pointer error, like </p>
  
  <pre><code>java.lang.NullPointerException
      at com.fetchinglife.application.modules.employee.view.EmployeeList.empList(EmployeeList.java:209)
  </code></pre>
  
  <p>My test class is :</p>
  
  <pre><code>public class EmployeeListTest extends TestCase {
  
      private EmployeeList employeeList = new EmployeeList();
      public static HibernateTemplate hibernateTemplate;
  
      @BeforeClass
      public void setUpBeforeClass() throws Exception {
  
                SessionFactory  sessionFactory = this.hibernateTemplate.getSessionFactory();
           Session session = sessionFactory.openSession();
            System.out.println("in before");
      }
  
          @Test
      public void testGetEmployees() throws HibernateException, SQLException {        
  
          List&lt;Employee&gt; employees = employeeList.getEmpList();       
          assertNotNull(employees);
      }
  </code></pre>
  
  <p>But Null pointer error for <code>testGetEmployees()</code>
  And my method in <code>EmployeeList</code> is :</p>
  
  <pre><code>public List&lt;Employee&gt; empList() {       
  
          try
          {
              @SuppressWarnings("unchecked")
              List &lt;Employee&gt; result =  hibernateTemplate.find("from Employee"); 
              return result;
          }
          finally { 
          }
      }
  </code></pre>
  
  <p>What I have to add more?I'm using JUnit 4.</p>
  
  <p>My EmployeeList.java here.</p>
  
  <pre><code>@Component("employeeList")
  @SessionScoped
  @Repository
  public class EmployeeList implements Serializable {
      private static final long serialVersionUID = -2417394435764260084L;
  
      public static HibernateTemplate hibernateTemplate;
  
      @Autowired
      public void setSessionFactory(SessionFactory sessionFactory)
      {
          this.hibernateTemplate = new HibernateTemplate(sessionFactory);
      }
  
      private Employee employee = new Employee();
  
      public List&lt;Employee&gt; getEmployees() throws HibernateException, SQLException {
          List&lt;Employee&gt; employees = new ArrayList&lt;Employee&gt;();
          System.out.println("in getEmployeess");
          employees.addAll(empList());    
          return employees;
  
      }
  
      public List&lt;Employee&gt; empList() {       
  
          System.out.println(" find all employees: ");    
          try
          {
  
              @SuppressWarnings("unchecked")
              List &lt;Employee&gt; result =  hibernateTemplate.find("from Employee"); 
              return result;
          }
          finally { 
          }
  
      }
  
      public Employee getEmployee() {
          return employee;
      }
  
      public void setEmployee(Employee employee) {
          this.employee = employee;
      }
  }
  </code></pre>
  
  <hr>
  
  <p>I have tried to retrieve hibernateTemplate but still error. I don't know where I got wrong. </p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = "/spring/servlet-config.xml")
  public class EmployeeListTest extends TestCase {
      private static HibernateTemplate hibernateTemplate;
  
      @Autowired
      private EmployeeList employeeList;
  
      @BeforeClass
      public static void setUpBeforeClass() throws Exception {
  
          SessionFactory  sessionFactory = hibernateTemplate.getSessionFactory();
          Session session = sessionFactory.openSession();     
      }
  
      @Test
      public void testGetEmployees() throws HibernateException, SQLException {
          List&lt;Employee&gt; employees = employeeList.getEmployees();     
          assertNotNull(employees);
      }
  }
  </code></pre>