Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 冬眠与幼树_Java_Hibernate_Junit - Fatal编程技术网

Java 冬眠与幼树

Java 冬眠与幼树,java,hibernate,junit,Java,Hibernate,Junit,我不熟悉JUnit测试,每当我尝试测试一个有hibernate查询结果空指针错误的方法时,比如 java.lang.NullPointerException at com.fetchinglife.application.modules.employee.view.EmployeeList.empList(EmployeeList.java:209) 我的测试班是: public class EmployeeListTest extends TestCase { privat

我不熟悉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);
    }
我还需要补充什么?我正在使用JUnit4

这里是My 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;
    }
}
@组件(“员工列表”)
@会议范围
@存储库
公共类EmployeeList实现可序列化{
私有静态最终长serialVersionUID=-2417394435764260084L;
公共静态HibernateTemplate HibernateTemplate;
@自动连线
public void setSessionFactory(SessionFactory SessionFactory)
{
this.hibernateTemplate=新的hibernateTemplate(sessionFactory);
}
私人雇员=新雇员();
public List getEmployees()引发HibernateeException,SQLException{
List employees=new ArrayList();
System.out.println(“in-getemployees”);
employees.addAll(employist());
返回员工;
}
公共列表雇员列表(){
System.out.println(“查找所有员工:”);
尝试
{
@抑制警告(“未选中”)
列表结果=hibernateTemplate.find(“来自员工”);
返回结果;
}
最后{
}
}
公共雇员getEmployee(){
返回员工;
}
公共作废集合雇员(雇员雇员){
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);
    }
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations=“/spring/servlet config.xml”)
公共类EmployeeListTest扩展了TestCase{
私有静态HibernateTemplate HibernateTemplate;
@自动连线
私人雇员名单;
@课前
public static void setUpBeforeClass()引发异常{
SessionFactory SessionFactory=hibernateTemplate.getSessionFactory();
Session Session=sessionFactory.openSession();
}
@试验
public void testGetEmployees()引发HibernateeException,SQLException{
List employeeList.getEmployees();
assertNotNull(员工);
}
}

您是否尝试调试术语:

List <Employee> result =  hibernateTemplate.find("from Employee"); 

它们在那里吗?

空指针异常是因为hibernateTemplate为空(检查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
@RunWith(SpringJUnit4ClassRunner.class)
@上下文配置(“”)
公共类EmployeeListTest扩展了TestCase{
@自动连线
私人雇员名单

employeeList.getEmployist()将变为null。但我尝试了一个简单的方法employeeList.check(),没有任何hibernate数据库查询,并且工作没有错误。尝试使用数据库时会导致错误。我已经发布了类。我们是否还需要在测试类中设置连接?或者我们测试其他方法的方法相同!现在出现了什么错误。/spring/servlet-config.xml文件在哪里?如果它在WEB-INF下,那么testcase将无法访问它。是的,它在WEB-INF下。是的,我在测试类中调用了类似于,
employeeList.getEmpList();
本身的方法。我确信我的
empList()是
不为null。DB表中是否有对象?是的,它在那里。我怀疑,在对DB查询进行测试时,我还需要添加什么?我们是否需要在测试类中设置连接?或者我们测试其他方法的方式相同!仅当(hibernateTemplate!=null)时才尝试访问Find方法@gkamal,但错误是:
类无法解析为类型
类无法解析为类型
。我正在使用junit-4.9.jar。@gkamal-正如您所说,我试图检索hibernateTemplate。我将代码放在上面的编辑部分。我哪里出错了?
List<Employee> employees = employeeList.getEmpList();
employeeList.getEmpList();
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("<put your spring config file here>")
public class EmployeeListTest extends TestCase {
    @Autowired
    private EmployeeList employeeList