排序列表上的AssertEquals总是返回false

时间:2015-01-28 03:21:20

标签: java unit-testing junit

我正在尝试编写一个单元测试来测试两个列表的排序。我所拥有的是我调用自定义排序比较器,然后将原始列表与排序列表进行比较。然后,我使用assertEquals来测试排序列表和原始列表是否匹配。

我们说我有一个简单的模型......患者。

患者有两个领域......姓名和年龄。

List<Patient> unOrdered = new ArrayList<Patient>();
List<Patient> ordered = new ArrayList<Patient>();

我用同样的三个病人填写这两个,然后正确地订购有序的病人。

ordered.add(new Patient("Chris Bacon", "45"));
ordered.add(new Patient("Charles Steak", "82"));
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列表进行排序。我在测试期间(对我而言)将该列表打印到控制台,然后执行...

assertEquals(ordered, unOrdered);

控制台以相同的顺序打印这些列表,但assertEquals返回false。我甚至尝试以相同的顺序创建两个完全相同的列表并尝试assertEquals并且它仍然返回false。

我不是Java专家,但是我在网上看到的文档assertEquals不仅检查列表中对象的相等性,还检查对象的顺序。所以...为什么它总是返回假?是不是assertEquals无法处理更复杂的对象,或者我做错了什么?

3 个答案:

答案 0 :(得分:2)

为了使两个列表被认为是相等的,一个列表的每个元素必须与另一个列表的相应元素进行比较,因此该测试完全依赖于Patient equals方法的实现。

答案 1 :(得分:0)

请参阅此演示代码,它可以正常运行。与你的比较,找到问题。

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);
    }
}

答案 2 :(得分:0)

比较列表时,基本上可以执行List如何执行。但是,对于大多数列表,您只需迭代所有元素并使用equals方法逐个比较元素。因此,在您的情况下,您需要为equals类提供Patient方法。并且,一如既往 - 还提供hashCode实施(这被认为是最佳实践)。

像这样的东西(如果你使用的是Java 8):

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());
    }
}

在旁注中,我建议不要将String用作age。考虑对年龄"2""10"进行排序。字符串值"10""2"之前,这可能不是意图。

此外,要对{​​{1}}个对象进行排序,您可以使用一些漂亮的Java 8功能:

Patient

请注意,您实际上并未对基础// 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

您可以在this excellent tutorial中阅读有关Java 8 Streams的更多信息,其中包括排序,过滤等。

相关问题