使用junit测试用例显示空指针异常

时间:2012-08-29 12:51:45

标签: java junit junit4

我正在尝试使用junit测试用例根据工资对员工对象进行排序。

这是我的员工排序类

package day4;
import day4.Employee;
public class EmployeesInfoWithSalary {
    private Employee[] employee;
    private int numberOfEmployees;
public EmployeesInfoWithSalary(Employee[] employee, int numberOfEmployees) {
    super();
    this.employee = employee;
    this.numberOfEmployees = numberOfEmployees;
}

public Employee[] getSortBasedOnSalary() {

    String temp;
    for (int iterator = 0; iterator < numberOfEmployees; iterator++) {
        int minSalary = employee[iterator].getSalary();
        int index = iterator;
        for (int comparator = iterator; comparator < numberOfEmployees; comparator++) {
            if (employee[comparator].getSalary() < minSalary) {
                index = comparator;
                minSalary = employee[comparator].getSalary();
            }

        }

        employee[index].setSalary(employee[iterator].getSalary());
        employee[iterator].setSalary(minSalary);
        temp = employee[index].getId();
        employee[index].setId(employee[iterator].getId());
        employee[iterator].setId(temp);
        temp = employee[index].getName();
        employee[index].setName(employee[iterator].getName());
        employee[iterator].setName(temp);

    }

    return employee;

}

}

员工对象类如下

package day4;

public class Employee {

private String id;
private String name;
private int salary;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getSalary() {
    return salary;
}

public void setSalary(int salary) {
    this.salary = salary;
}

}

junit测试案例的testemployee薪资等级如下

package day4;

import static org.junit.Assert.*;
import day4.Employee;

import org.junit.Test;

public class TestEmployeeInfoWithSalary {

@Test
public void testGetSortBasedOnSalary() {

Employee[] employee = new Employee[5];

employee[0].setName("pratap");
employee[1].setName("aruna");
employee[2].setName("satyam");
employee[3].setName("krishna");
employee[4].setName("siva");

employee[0].setId("k0100");
employee[1].setId("k0101");
employee[2].setId("k0102");
employee[3].setId("k0103");
employee[4].setId("k0104");
employee[0].setSalary(10000);
employee[1].setSalary(1000);
employee[2].setSalary(8000);
employee[3].setSalary(6000);
employee[4].setSalary(9000);
EmployeesInfoWithSalary employeeInfoWithSalary= new EmployeesInfoWithSalary(employee, 5);

employee[4].setName("pratap");
employee[0].setName("aruna");
employee[2].setName("satyam");
employee[1].setName("krishna");
employee[3].setName("siva");

employee[4].setId("k0100");
employee[0].setId("k0101");
employee[2].setId("k0102");
employee[1].setId("k0103");
employee[3].setId("k0104");
employee[4].setSalary(10000);
employee[0].setSalary(1000);
employee[2].setSalary(8000);
employee[1].setSalary(6000);
employee[3].setSalary(9000);
assertArrayEquals(employee,employeeInfoWithSalary.getSortBasedOnSalary());

}

}

日志显示空点表达式的错误..

任何人都可以帮助我.. 感谢..

2 个答案:

答案 0 :(得分:7)

我怀疑这是NPE的路线。

// creates an array full of null values.
Employee[] employee = new Employee[5];

employee[0].setName("pratap");

您需要将Employee对象添加到数组中的每个元素。

更好的方法是使用构造函数来获取所有需要的字段。

Employee[] employee = {
    new Employee("pratap", "k0100", 10000),
    new Employee("aruna",  "k0101",  1000),
    new Employee("satyam", "k0102",  8000),
    new Employee("krishna","k0103",  6000),
    new Employee("siva",   "k0104",  9000) };

答案 1 :(得分:0)

 Employee[] employee = new Employee[5];

对于数组中的每个索引,您需要初始化Employee对象。

 employee[0] = new Employee(); etc