我在测试方法( displayEmployees )时遇到问题,该方法返回对象的列表。
public class Task {
static List<Employee> employeeList;
public static void main(String[] args) {
Employee employee1 = new Employee("Dobrobaba", "Irina", "Ivanovna",
"Moskva", 1900, 6);
Employee employee2 = new Employee("Shmal", "Anna", "Nikolaevna",
"Krasnodar", 2017, 8);
Employee employee3 = new Employee("Kerimova", "Niseimhalum", "Magomedmirzaevna",
"New-York", 2017, 3);
Employee employee4 = new Employee("Dobryden", "Yuri", "Viktorovich",
"Auckland", 2014, 11);
Employee employee5 = new Employee("Lopata", "Leonid", "Nikolaevich",
"Beijing", 2014, 11);
employeeList = new ArrayList<>(Arrays.asList(employee1, employee2, employee3, employee4, employee5));
}
/**
* Prints employees' information, which have worked more than 'n' year(s) for now.
*
* @param n years quantity
*/
public static List<Object> displayEmployees(int n) {
List<Employee> requiredList = new ArrayList<>();
employeeList.forEach(employee -> {
if (Year.now().getValue() - employee.getEmploymentYear() >= n) {
requiredList.add(employee);
}
});
return Collections.singletonList(requiredList);
}
}
这就是我要进行测试的方法,将2个列表相互比较:
public class TaskTest {
public static void main(String[] args) {
displayEmployeesTest();
}
public static void assertEquals(String testName, List<Object> expected, List<Object> actual) {
if (expected.equals(actual)) {
System.out.println(testName + " passed");
} else {
System.out.println(testName + " failed: expected " + expected + ", actual " + actual);
}
}
private static void displayEmployeesTest() {
List<Object> expectedList;
Employee employee1 = new Employee("Dobrobaba", "Irina", "Ivanovna",
"Moskva", 1900, 6);
Employee employee2 = new Employee("Shmal", "Anna", "Nikolaevna",
"Krasnodar", 2017, 8);
Employee employee3 = new Employee("Kerimova", "Niseimhalum", "Magomedmirzaevna",
"New-York", 2017, 3);
Employee employee4 = new Employee("Dobryden", "Yuri", "Viktorovich",
"Auckland", 2014, 11);
Employee employee5 = new Employee("Lopata", "Leonid", "Nikolaevich",
"Beijing", 2014, 11);
expectedList = new ArrayList<>(Arrays.asList(employee1, employee4, employee5));
assertEquals("displayEmployeesTest", expectedList, displayEmployees(3));
}
}
问题是-运行 TaskTest 时,出现错误“ java.lang.NullPointerException” 。
该错误发生的原因是什么? 谢谢您的关注!
答案 0 :(得分:1)
要比较两个列表,可以使用列表的equals
方法。但是,为了使用此方法,列表的内容类必须覆盖equals
。
employeeList1.equals(employeeList2)
并且您的Employee类必须重写等于:
public class Employee {
private String name;
private int id;
...
//getters and setters
...
public boolean equals(Object obj) {
if(obj == null) return false;
if(obj instanceof Employee) {
Employee e = (Employee) obj;
return this.name.equals(e.name) && this.id == e.id;
} else return false;
}
}
建议使用eclipse或Intellij这样的IDE来实现equals
由于static List<Employee> employeeList
正在用您的测试方法从未调用过的main方法中初始化,因此您得到了一个空指针。
为了创建美观而整洁的测试,您应该使用Junit。检查以下内容:http://www.vogella.com/tutorials/JUnit/article.html
希望这会有所帮助
答案 1 :(得分:1)
您调用Task.displayEmployees
时不会调用Task.main
,因此Task中的employeeList
字段不会被初始化
答案 2 :(得分:0)
如果要比较创建的对象,则可以让该类实现可比较的接口。该方法只有一个方法,即comparTo,并重写该方法以根据给定的属性比较员工。使用预定义的comparTo可能无法像您想要比较员工那样