在 JUnit 中检查两个列表是否相同

Checking whether two Lists are the same in JUnit

在这个 class 中,我试图测试我是否从 interiorPoints class 得到了正确的结果。 interiorPoints class 是一种 returns 位于多边形内的坐标列表的方法。

下面我正在检查预期结果是否与实际结果相同,在这种情况下它们应该相同,但由于我无法弄清楚的原因,测试似乎失败了。 有人可以帮忙吗

public class TestInteriorPoints {
        private InteriorPoints interiorPoints;
        List<Point> TestPoints;
        List<Point> convexHull;


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

            TestPoints = new ArrayList<Point>();
            TestPoints.add(new Point(300,200));
            TestPoints.add(new Point(600,500));
            TestPoints.add(new Point(100,100));
            TestPoints.add(new Point(200,200));
            TestPoints.add(new Point(100,500));
            TestPoints.add(new Point(600,100));

            convexHull = new ArrayList<Point>();
            convexHull.add(new Point(100,100));
            convexHull.add(new Point(100,500));
            convexHull.add(new Point(600,500));
            convexHull.add(new Point(600,100));

        }

        @Test
        public void testInteriorPoints() {
            List<Point> expectedResult = new ArrayList<Point>();
            expectedResult.add(new Point(300,200));
            expectedResult.add(new Point(200,200));

            List<Point> actualResult = new ArrayList<Point>();
            actualResult = interiorPoints.interiorPoints(convexHull, TestPoints);

            assertEquals("TEST5: Check for interior points", expectedResult, actualResult);
            //assertTrue(expectedResult.containsAll(actualResult) && actualResult.containsAll(expectedResult));
        }

    }

要使 assertEquals 在您的 List Point 上工作,需要实现 equals(因此 hashCode 也是如此)。

为此,我建议 org.apache.lang3.builder.EqualsBuilder 和 org.apache.lang3.builder.HashCodeBuilder

编辑:

例如

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

public class Person {

    private String name;

    private Long ageInSeconds;

    public Person(String name, Long ageInSeconds) {
        this.name = name;
        this.ageInSeconds = ageInSeconds;
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(13, 31) // pick two hard coded odd numbers, preferably different for each class
                .append(name)
                .append(ageInSeconds)
                .build();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (obj.getClass() != getClass()) {
            return false;
        }
        Person rhs = (Person) obj;
        return new EqualsBuilder()
                // .appendSuper(super.equals(obj)) not needed here, but would be used if example Person extended another object
                .append(name, rhs.name)
                .append(ageInSeconds, rhs.ageInSeconds)
                .isEquals();
    }
}