我是JUnit testing.Whenever的新手,我试着测试一个具有hibernate查询结果空指针错误的方法,如
java.lang.NullPointerException
at com.fetchinglife.application.modules.employee.view.EmployeeList.empList(EmployeeList.java:209)我的考试课是:
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指针错误是:
public List<Employee> empList() {
try
{
@SuppressWarnings("unchecked")
List <Employee> result = hibernateTemplate.find("from Employee");
return result;
}
finally {
}
}我还需要添加什么呢?我使用的是JUnit 4。
我的EmployeeList.java在这里。
@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,但仍然出错。我不知道我哪里弄错了。
@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);
}
}发布于 2011-12-22 06:50:29
null指针异常是因为hibernateTemplate为null (检查行EmployeeList.java:209)。之所以会发生这种情况,是因为您正在测试类中创建一个新的EmployeeList对象--应该从spring上下文中检索它。
您需要配置测试以创建spring应用程序上下文,然后将EmployeeList作为依赖项注入。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("<put your spring config file here>")
public class EmployeeListTest extends TestCase {
@Autowired
private EmployeeList employeeList发布于 2011-12-22 05:22:47
您试过调试术语:
List <Employee> result = hibernateTemplate.find("from Employee"); 那里有什么东西吗?如果列表中没有员工,是否创建了列表?那么,它是空的!基本上,您不应该在unitest中进行数据库访问操作,这些操作花费了您太多的时间,另一种方法是创建模拟ups,在最统一的环境中模拟您的数据对象。
读:How to simulate a DB for testing (Java)?
这条线呢?
List<Employee> employees = employeeList.getEmpList();我认为public List<Employee> empList()方法必须有get和set方法,这样您就可以调用:
employeeList.getEmpList();他们在那里吗?
https://stackoverflow.com/questions/8599668
复制相似问题