i recently started playing around with tdd and ran into a problem where i do not understand why one thing is working and the other one doesnt.
the following code works for me:
public class Ant {
public Ant(Point startLocation, Point hive) {
this.currentLocation = new Point(startLocation);
this.hive = new Point(hive);
}
public void goHome() {
if (hive.x > currentLocation.x) {
currentLocation.x++;
} else if (hive.x < currentLocation.x){
currentLocation.x--;
}
if (hive.y > currentLocation.y) {
currentLocation.y++;
} else if (hive.y < currentLocation.y){
currentLocation.y--;
}
}
}
The corresponding test:
@DataProvider(name = "goneHome")
public static Object[][] goHome() {
return new Object[][] {
{new Point(2,1), new Point(3,2), new Point(7,8)},
{new Point(20,1), new Point(19,2), new Point(7,8)},
{new Point(23,10), new Point(22,9), new Point(7,8)},
{new Point(2,10), new Point(3,9), new Point(7,8)},
{new Point(2,8), new Point(3,8), new Point(7,8)},
{new Point(7,1), new Point(7,2), new Point(7,8)}
};
}
@Test(dataProvider = "goneHome")
public void testGoHome(Point currentPosition, Point nextPosition, Point hive)
throws Exception {
Ant ant = new Ant(currentPosition, hive);
ant.move();
assertEquals(ant.getCurrentLocation(), nextPosition);
}
the test fails if i change the ant constructor like this:
public Ant(Point startLocation, Point hive) {
this.currentLocation = startLocation;
this.hive = hive;
}
By failing i mean that the test with the first two sets of the DataProvider work correctly, the rest is failing/not finishing. Although i am not quite sure what failed. If i remove the first two sets of data in the DataProvider, still only the first two datasets (which where the 3rd and 4th data set before) do not fail.
I use IntelliJ and the symbol besides the "failed" test is still the "loading icon".
Debugging each single test case shows that the points are set correctly. Removing the assert from the test does not change anything.
Can someone explain this behavior to me please?
Thanks in advance
Egon
Edit: corrected the version of the constructor that failed
Maybe it's a bug in IntelliJ IDEA. Sometimes I also facing with this problem. Unfortunatelly it's still (2014-11-24) unresolved: https://youtrack.jetbrains.com/issue/IDEA-100752
Try run your tests with alternate runner (as Maven goal, for instance).