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

标签 java eclipse unit-testing junit

在这个类中,我试图测试我是否从 InteriorPoints 类获得正确的结果。 InteriorPoints 类是一种返回位于多边形内的坐标列表的方法。

下面我正在检查预期结果是否与实际结果相同,在本例中它们应该是相同的,但测试似乎因我无法弄清楚的原因而失败。 有人可以帮忙吗?

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在你的列表点上工作,需要实现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();
    }
}

关于java - JUnit 中检查两个列表是否相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28530690/

相关文章:

java - 为什么 System.err 在 Eclipse 中比 System.out 慢?

android - 如何将测试文件夹添加到较旧的 Android Studio 项目

unit-testing - 无法使用 VSTest.Console.exe 运行 WP81 单元测试

ios - 是否有与 Mockito 类似的框架用于在 Kotlin MPP 中测试共享代码或 iOS 代码?

java - 将字符串地址转换为 lat 和 lng 大约需要 1 秒

java - Spring 3 和 Hibernate 4 道

java - throws 子句有什么意义?

java - 无法创建 SOAP 连接工厂 : Provider com. sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnectionFactory 未找到

java - 将共享/安装的 block 存储与 Xodus 一起使用

java - Android Studio 自动补全