Reputation: 73
In this class I'm trying to tests whether i get a correct result from the interiorPoints class. The interiorPoints class is a method that returns a lists of coordinates that lie within a polygon.
Below I'm checking whether the expected result is the same as the actual result which in this case they should be but the test seems to fail for a reason i can't figure out. Can someone help please.
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));
}
}
Upvotes: 0
Views: 56
Reputation: 5222
For assertEquals to work on your List Point needs to have equals implemented (and so hashCode as well).
To do this I would recommend org.apache.lang3.builder.EqualsBuilder and org.apache.lang3.builder.HashCodeBuilder
edit:
For example
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();
}
}
Upvotes: 2