Reputation: 1757
I am new to using JUnit so it's likely I'm missing something completely basic, however the following is the output I receive:
Testcase: testDateFromDayOfYear(p3.DateADTest): FAILED
expected: p3.DateAD<Saturday, 1 January, 2000> but was: p3.DateAD<Saturday, 1 January, 2000>
junit.framework.AssertionFailedError: expected: p3.DateAD<Saturday, 1 January, 2000> but was: p3.DateAD<Saturday, 1 January, 2000>
at p3.DateADTest.testDateFromDayOfYear(DateADTest.java:85)
I also compared the two objects:
System.out.println("*******************************************");
System.out.println(result.getClass());
System.out.println(expResult.getClass());
System.out.println("*******************************************");
*******************************************
class p3.DateAD
class p3.DateAD
*******************************************
Could someone please offer some advice?
Complete code for test method:
@Test
public void testDateFromDayOfYear() {
System.out.println("dateFromDayOfYear");
short dayOfYear = (short)1;
short year = (short)2000;
DateAD instance = new DateAD();
DateAD expResult = new DateAD(dayOfYear, (short)1, year);
DateAD result = instance.dateFromDayOfYear(dayOfYear, year);
System.out.println("*******************************************");
System.out.println(result.getClass());
System.out.println(expResult.getClass());
System.out.println("*******************************************");
assertEquals(expResult, result);
}
Equals Method:
public boolean equals(Object inputDate) {
if (this.year == inputDate.year &&
this.dayOfYear == inputDate.dayOfYear) {
return true;
}
else {
return false;
}
}
Upvotes: 0
Views: 4456
Reputation: 44439
assertEquals
will call equals
which refers to the memory location of reference types. If you can override the Equals
(and getHashCode
) methods of DateAD
you should do that.
Otherwise, perform an equals check on the hours, minutes, days, etc which make up the date (or time in milli/nanoseconds).
public boolean equals(Object o){
if(o == null) { return false; }
if(!(o instanceOf DateAD)) { return false; }
DateAD date = (DateAD) o;
return (this.year == date.year && this.dayOfYear == inputDate.dayOfYear);
}
Upvotes: 1