首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Hibernate和JUnit

Hibernate和JUnit
EN

Stack Overflow用户
提问于 2011-12-22 05:09:46
回答 2查看 3.6K关注 0票数 2

我是JUnit testing.Whenever的新手,我试着测试一个具有hibernate查询结果空指针错误的方法,如

代码语言:javascript
运行
复制
java.lang.NullPointerException
    at com.fetchinglife.application.modules.employee.view.EmployeeList.empList(EmployeeList.java:209)

我的考试课是:

代码语言:javascript
运行
复制
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<Employee> employees = employeeList.getEmpList();       
        assertNotNull(employees);
    }

但是,对于testGetEmployees()和我在EmployeeList中的方法,Null指针错误是:

代码语言:javascript
运行
复制
public List<Employee> empList() {       

        try
        {
            @SuppressWarnings("unchecked")
            List <Employee> result =  hibernateTemplate.find("from Employee"); 
            return result;
        }
        finally { 
        }
    }

我还需要添加什么呢?我使用的是JUnit 4。

我的EmployeeList.java在这里。

代码语言:javascript
运行
复制
@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<Employee> getEmployees() throws HibernateException, SQLException {
        List<Employee> employees = new ArrayList<Employee>();
        System.out.println("in getEmployeess");
        employees.addAll(empList());    
        return employees;

    }

    public List<Employee> empList() {       

        System.out.println(" find all employees: ");    
        try
        {

            @SuppressWarnings("unchecked")
            List <Employee> result =  hibernateTemplate.find("from Employee"); 
            return result;
        }
        finally { 
        }

    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }
}

我试图检索hibernateTemplate,但仍然出错。我不知道我哪里弄错了。

代码语言:javascript
运行
复制
@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<Employee> employees = employeeList.getEmployees();     
        assertNotNull(employees);
    }
}
EN

回答 2

Stack Overflow用户

发布于 2011-12-22 06:50:29

null指针异常是因为hibernateTemplate为null (检查行EmployeeList.java:209)。之所以会发生这种情况,是因为您正在测试类中创建一个新的EmployeeList对象--应该从spring上下文中检索它。

您需要配置测试以创建spring应用程序上下文,然后将EmployeeList作为依赖项注入。

代码语言:javascript
运行
复制
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("<put your spring config file here>")
public class EmployeeListTest extends TestCase {
    @Autowired
    private EmployeeList employeeList
票数 3
EN

Stack Overflow用户

发布于 2011-12-22 05:22:47

您试过调试术语:

代码语言:javascript
运行
复制
List <Employee> result =  hibernateTemplate.find("from Employee"); 

那里有什么东西吗?如果列表中没有员工,是否创建了列表?那么,它是空的!基本上,您不应该在unitest中进行数据库访问操作,这些操作花费了您太多的时间,另一种方法是创建模拟ups,在最统一的环境中模拟您的数据对象。

读:How to simulate a DB for testing (Java)?

这条线呢?

代码语言:javascript
运行
复制
List<Employee> employees = employeeList.getEmpList();

我认为public List<Employee> empList()方法必须有get和set方法,这样您就可以调用:

代码语言:javascript
运行
复制
employeeList.getEmpList();

他们在那里吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8599668

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档