簡體   English   中英

比較Junit中的兩個observable列表

[英]Comparing two observableLists in Junit

我試圖使用assertEquals檢查兩個列表是否相同,這很好但是當試圖將列表更改為observableList時,測試失敗了。

那么如何比較JUnit中的兩個可觀察列表呢? 我想僅比較列表的內容。 基本上,這些observableLists包含Point對象,在Point類中我有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);
    }

這是點類

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類型。 因此Point類中的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();
    }

這將使測試通過

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM