gpt4 book ai didi

java - hibernate 和 JUnit

转载 作者:行者123 更新时间:2023-12-04 06:03:55 25 4
gpt4 key购买 nike

我是 JUnit 测试的新手。每当我尝试测试具有 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 中的方法是 :
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);
}
}

最佳答案

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

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

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("<put your spring config file here>")
public class EmployeeListTest extends TestCase {
@Autowired
private EmployeeList employeeList

关于java - hibernate 和 JUnit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8599668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com