我正在尝试根据年龄对员工进行排序(简化示例),但我无法让我的单元测试正常工作。
public class Employee{
private int age;
public void setAge(int age){
this.age=age;
}
public int getAge(){
return this.age;
}
}
我的比较器如下所示:
class AgeComparator implements Comparator<Employee>{
public int compare(Employee emp1, Employee emp2){
int emp1Age = emp1.getAge();
int emp2Age = emp2.getAge();
if(emp1Age > emp2Age)
return 1;
else if(emp1Age < emp2Age)
return -1;
else
return 0;
}
}
我的单元测试:
public class AgeComparatorTest {
@Test
public void testAge(){
Employee e1 = new Employee();
e1.setAge(4);
Employee e2 = new Employee();
e2.setAge(7);
List<Employee> employeeList = new ArrayList<Employee>();
employeeList.add(e1);
employeeList.add(e2);
Collections.sort(employeeList, new AgeComparator());
Employee actual = employeeList.get(0);
Assert.assertEquals(e2.getAge(), actual.getAge());
}
}
我希望 7 岁的员工在 4 岁之前,但我得到了。
junit.framework.AssertionFailedError: 预期:<7> 但是是:<4>