比较 Junit 中的两个 observableLists

Comparing two observableLists in Junit

我尝试使用 assertEquals 检查两个列表是否相同,这工作得很好,但是当尝试将列表更改为 observableList 时,测试失败。

那么如何比较 JUnit 中的两个可观察列表?我只想比较列表的内容。基本上,这些 observableLists 包含 Point 对象,在 Point class 中我有 hashCodeBuilder 和 equalsBuilder 方法。列表比较需要 hashCode()equals() 方法,但我不确定 ObservableList 是否需要它们。

public class TestClass {
    private MyClass myclass;
    ObservableList<Point> TestPoints = FXCollections.observableArrayList();

    /**
     * @throws java.lang.Exception
     */
    @Before
    public void setUp() throws Exception {
        myclass = new MyClass();

        TestPoints.add(new Point(300.0,200.0));
        TestPoints.add(new Point(600.0,500.0));
        TestPoints.add(new Point(100.0,100.0));
        TestPoints.add(new Point(200.0,200.0));
        TestPoints.add(new Point(100.0,500.0));
        TestPoints.add(new Point(600.0,100.0));
    }

    @Test
    public void testClass() {
        ObservableList<Point> expectedResult = FXCollections.observableArrayList();
        expectedResult.add(new Point(100.0,100.0));
        expectedResult.add(new Point(100.0,500.0));
        expectedResult.add(new Point(600.0,500.0));
        expectedResult.add(new Point(600.0,100.0));

        ObservableList<Point> actualResult = FXCollections.observableArrayList();
        actualResult = myclass.giftWrapping(TestPoints);

        assertEquals(expectedResult, actualResult);
    }

这是重点class

public class Point {

    private final DoubleProperty x;
    private final DoubleProperty y;

    public Point() {
        this(0, 0);
    }

    public Point(double x, double y) {
        this.x = new SimpleDoubleProperty(x);
        this.y = new SimpleDoubleProperty(y);
    }
@Override
public int hashCode() {
    HashCodeBuilder hashCodeBuilder = new HashCodeBuilder();
    hashCodeBuilder.append(x);
    hashCodeBuilder.append(y);
    return hashCodeBuilder.toHashCode();
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (!(obj instanceof Point)) {
        return false;
    }
    Point other = (Point) obj;
    EqualsBuilder equalsBuilder = new EqualsBuilder();
    equalsBuilder.append(x, other.x);
    equalsBuilder.append(y, other.y);
    return equalsBuilder.isEquals();
}

如果我使用 List,这会起作用,但如果我使用 observableList,则不起作用

基本上这个问题的问题在于 equals 和 hashCode 方法。变量 x 和变量 y 必须转换为双精度值,因为它们最初是 DoubleProperty 类型。所以Pointclass中的equals和hashCode方法应该如下

@Override
    public int hashCode() {
        HashCodeBuilder hashCodeBuilder = new HashCodeBuilder();
        hashCodeBuilder.append(x.doubleValue());
        hashCodeBuilder.append(y.doubleValue());
        return hashCodeBuilder.toHashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof Point)) {
            return false;
        }
        Point other = (Point) obj;
        EqualsBuilder equalsBuilder = new EqualsBuilder();
        equalsBuilder.append(x.doubleValue(), other.x.doubleValue());
        equalsBuilder.append(y.doubleValue(), other.y.doubleValue());
        return equalsBuilder.isEquals();
    }

这将使测试能够通过