在这堂课中我试图测试我是否从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));
}
}
答案 0 :(得分:2)
要使assertEquals处理List List,需要实现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();
}
}