I have a problem in hitting the yearsToSubtract == Long.MIN_VALUE branch when the value of yearsToSubtract is Long.MIN_VALUE.
public Period minusYears(long yearsToSubtract) {
return (yearsToSubtract == Long.MIN_VALUE ?plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-yearsToSubtract));
}
public Period plusYears(long yearsToAdd) {
if (yearsToAdd == 0) {
return this;
}
return create(Math.toIntExact(Math.addExact(years, yearsToAdd)), months, days);
}
this is my plusYears Function, my create function is just a getter function which sets the value of years, Months and Days
this is my Test case for covering the branch when the value of yearsToSubtract is Long.MIN_VALUE
@Test
public void testIfMinusYears() {
Period p = Period.of(1, 1, 1);
try {
p.minusYears(Long.MIN_VALUE);
} catch (ArithmeticException e) {
}
}
This is my test case to hit the other branch when yearsToSubtract is a value which is not equal to Long.MIN_VALUE
@Test
public void testMinusYears() {
Period p = Period.of(0, 0, 0);
p.minusYears(-100);
}
of(years, Months, days) is a function which sets the values of Year, Month and Days
can someone please tell me how to hit that branch, this is for an assignment in my class, I have tried in all possible ways, but wasn't able to figure out how to hit that branch.
Thanks