I have a constructor and accesor method which are supposed to not have any privacy leaks but when I run my JUnit tests I am not getting my intended output.
This is my constructor
public Date(int month, int day, int year){
if(day < 1 || day > 31){
System.out.println("invalid day: " + day);
System.exit(0);
}else if(month < 1 || month > 12){
System.out.println("invalid month: " + month);
System.exit(0);
}else if(year < 2014 || year > 2024){
System.out.println("invalid year: " + year);
System.exit(0);
}
this.month = month;
this.day = day;
this.year = year;
}
And this is my accesor method
public void setDay(int day) {
if (day >= 1 || day <= 31)
this.day = day;
}
The JUnit test I ran was this
@Test
public void OrderDatePrivacyLeaks()
{
// make sure date was copied when set
Date d = new Date(6,12,2017);
Order b = new Order(new Money(2,33), d, "ACME Company", "widget");
d.setDay(10);
Date billDate = b.getOrderDate();
assertEquals(12, billDate.getDay());
}
And the setDay
should not be able to change the values of b
so the expected output is 12 yet I am getting 10. Could someone tell me what the issue is?