Java 排序列表上的AssertEquals始终返回false
我试图编写一个单元测试来测试两个列表的排序。我所做的是调用自定义排序比较器,然后将原始列表与排序列表进行比较。然后,我使用assertEquals测试排序列表和原始列表是否匹配 假设我有一个简单的模型。。。病人 病人有两个领域。。。姓名和年龄Java 排序列表上的AssertEquals始终返回false,java,unit-testing,junit,Java,Unit Testing,Junit,我试图编写一个单元测试来测试两个列表的排序。我所做的是调用自定义排序比较器,然后将原始列表与排序列表进行比较。然后,我使用assertEquals测试排序列表和原始列表是否匹配 假设我有一个简单的模型。。。病人 病人有两个领域。。。姓名和年龄 List<Patient> unOrdered = new ArrayList<Patient>(); List<Patient> ordered = new ArrayList<Patient>(); 然
List<Patient> unOrdered = new ArrayList<Patient>();
List<Patient> ordered = new ArrayList<Patient>();
然后我填好顺序表,按年龄顺序递增
unOrdered.add(new Patient("Matt Pork", "32"));
unOrdered.add(new Patient("Chris Bacon", "45"));
unOrdered.add(new Patient("Charles Steak", "82"));
因此,在我的单元测试中,我编写了一个Collections.sort和一个自定义比较器,以按年龄升序排列无序列表。我在测试期间(为我)将该列表打印到控制台,然后执行
assertEquals(ordered, unOrdered);
控制台以相同的顺序打印这些列表,但assertEquals返回false。我甚至尝试过以相同的顺序创建两个完全相同的列表并尝试assertEquals,但它仍然返回false
我不是Java专家,但从我在线阅读的内容来看,assertEquals文档不仅检查列表中的对象是否相等,还检查对象的顺序。所以为什么它总是返回false?是assertEquals无法处理更复杂的对象,还是我做错了什么?要将两个列表视为相等,一个列表的每个元素必须与另一个列表的对应元素进行相等比较,因此,此测试完全依赖于
Patient
equals方法的实现。请查看此演示代码,它可以毫无错误地运行。将其与您的进行比较,找出问题所在
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.junit.Test;
class Patient {
private String name;
private String age;
/**
* Constructor
* @param name
* @param age
*/
public Patient(String name, String age) {
super();
this.name = name;
this.age = age;
}
/**
* This is the override of super method.
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((age == null) ? 0 : age.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
/**
* This is the override of super method.
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Patient other = (Patient) obj;
if (age == null) {
if (other.age != null)
return false;
} else if (!age.equals(other.age))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public String getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(String age) {
this.age = age;
}
}
/**
* Unit test for simple App.
*/
public class AppTest {
/**
* Rigourous Test :-)
*/
@Test
public void testApp() {
List<Patient> unOrdered = new ArrayList<Patient>();
List<Patient> ordered = new ArrayList<Patient>();
ordered.add(new Patient("Charles Steak", "82"));
ordered.add(new Patient("Chris Bacon", "45"));
ordered.add(new Patient("Matt Pork", "32"));
unOrdered.add(new Patient("Matt Pork", "32"));
unOrdered.add(new Patient("Chris Bacon", "45"));
unOrdered.add(new Patient("Charles Steak", "82"));
Collections.sort(unOrdered, new Comparator<Patient>(){
/**
* This method is just for demo. Not well defined.
*
* @param o1
* @param o2
* @return
*/
@Override
public int compare(Patient o1, Patient o2) {
return o1.getName().compareTo(o2.getName());
}});
assertEquals(ordered, unOrdered);
}
}
导入静态org.junit.Assert.assertEquals;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.Comparator;
导入java.util.List;
导入org.junit.Test;
班级病人{
私有字符串名称;
私弦时代;
/**
*建造师
*@param name
*@param年龄
*/
公共患者(字符串名称、字符串年龄){
超级();
this.name=名称;
这个。年龄=年龄;
}
/**
*这是超级方法的重写。
*@see java.lang.Object#hashCode()
*/
@凌驾
公共int hashCode(){
最终整数素数=31;
int结果=1;
result=prime*result+((age==null)?0:age.hashCode();
result=prime*result+((name==null)?0:name.hashCode();
返回结果;
}
/**
*这是超级方法的重写。
*@see java.lang.Object#equals(java.lang.Object)
*/
@凌驾
公共布尔等于(对象obj){
if(this==obj)
返回true;
if(obj==null)
返回false;
如果(getClass()!=obj.getClass())
返回false;
患者其他=(患者)obj;
如果(年龄==null){
if(other.age!=null)
返回false;
}如果(!age.equals(other.age))
返回false;
if(name==null){
if(other.name!=null)
返回false;
}如果(!name.equals(other.name))
返回false;
返回true;
}
/**
*@返回名称
*/
公共字符串getName(){
返回名称;
}
/**
*@param name要设置的名称
*/
公共void集合名(字符串名){
this.name=名称;
}
/**
*@回归时代
*/
公共字符串getAge(){
回归年龄;
}
/**
*@param age要设置的年龄
*/
公共无效设置(字符串期限){
这个。年龄=年龄;
}
}
/**
*简单应用程序的单元测试。
*/
公共类应用程序测试{
/**
*严格试验:-)
*/
@试验
公共无效测试{
List unOrdered=new ArrayList();
List ordered=new ArrayList();
订购。添加(新患者(“查尔斯牛排”、“82”);
订购。添加(新患者(“Chris Bacon”,“45”));
订购。添加(新患者(“Matt Pork”,“32”));
无序。添加(新患者(“Matt Pork”,“32”));
无序。添加(新患者(“Chris Bacon”,“45”));
无序。添加(新患者(“查尔斯牛排”、“82”);
Collections.sort(无序,新的Comparator(){
/**
*此方法仅用于演示。未定义好。
*
*@param o1
*@param o2
*@返回
*/
@凌驾
公共int比较(患者o1、患者o2){
返回o1.getName().compareTo(o2.getName());
}});
资产质量(有序、无序);
}
}
在比较列表时,基本上取决于列表的执行方式。但是,对于大多数列表,您只需迭代所有元素,并使用equals
方法逐个比较元素。因此,在您的案例中,您需要为患者
类提供一个等于
方法。而且,一如既往,还要提供一个hashCode
实现(这被认为是最佳实践)
类似这样的内容(如果您使用的是Java 8):
另一方面,我不建议使用字符串作为age
since。考虑整理年龄>代码> 2”<代码>或<代码> 10”< /代码>。字符串值“10”
位于“2”
之前,这可能不是目的
此外,要对Patient
对象进行排序,您可以使用一些漂亮的Java 8功能,如下所示:
// An unordered list of all patients
List<Patient> allPatients = Arrays.asList(
new Patient("Matt Pork", "32"),
new Patient("Chris Bacon", "45"),
new Patient("Charles Steak", "82")
);
// Sort by name
List<Patient> sortedByName = allPatients.stream()
.sorted(Comparator.comparing(Patient::getName))
.collect(Collectors.toList());
// Sort by age
List<Patient> sortedByAge = allPatients.stream()
.sorted(Comparator.comparing(Patient::getAge))
.collect(Collectors.toList());
//所有患者的无序列表
List allPatients=Arrays.asList(
新病人(“马特·波克”,“32”),
新患者(“Chris Bacon”,“45”),
新病人(“查尔斯牛排”,“82”)
);
//按名称排序
List sortedByName=allPatients.stream()
.排序(比较器.比较(患者::getName))
.collect(Collectors.toList());
//按年龄分类
List sortedbage=allPatients.stream()
.已排序(比较器.比较(患者::getAge))
.collect(Collectors.toList());
public class Patient {
private final String age;
private final String name;
public Patient(final String name, final String age) {
this.name = name;
this.age = age;
}
public String getAge() {
return age;
}
public String getName() {
return name;
}
@Override
public boolean equals(Object o) {
if (o instanceof Patient) {
Patient other = (Patient) o;
return Objects.equals(getName(), other.getName()) && Objects.equals(getAge(), other.getAge());
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(getName(), getAge());
}
}
// An unordered list of all patients
List<Patient> allPatients = Arrays.asList(
new Patient("Matt Pork", "32"),
new Patient("Chris Bacon", "45"),
new Patient("Charles Steak", "82")
);
// Sort by name
List<Patient> sortedByName = allPatients.stream()
.sorted(Comparator.comparing(Patient::getName))
.collect(Collectors.toList());
// Sort by age
List<Patient> sortedByAge = allPatients.stream()
.sorted(Comparator.comparing(Patient::getAge))
.collect(Collectors.toList());