I have a Util class with 1 method which calculates age using joda-time:
public static int getAge(LocalDate birthdate) {
LocalDate today = new LocalDate();
Period period = new Period(birthdate, today, PeriodType.yearMonthDay());
return period.getYears();
}
I've written an extensive JUnit test class but how do I make it time-independent? If I create a test case to calculate someone's age if they were born today in 1970 then the result would be 46 years old. But 1 year from now if i run the test case it would fail because the result would be 47.
So how do I make these test cases time-independent. I was thinking of having some sort of calendar interface that the test cases would create a date object from. I also stumbled across this post which is another possible solution to this but I'm not really sure how to go about it.