I'm making an application for division, and it has a validator class. When I wrote some tests, Jacoco showed a line saying that it's not covered by tests, but it is. Code of Validator:
public class ValidatorImpl implements Validator {
@Override
public void validate(int dividend, int divisor) {
if (divisor == 0) {
throw new IllegalArgumentException("You can`t divide by ZERO!");
}
if (divisor < 0) {
throw new IllegalArgumentException("Divisor must be above zero");
}
if (dividend < 0) {
throw new IllegalArgumentException("Dividend must be above zero");
}
}
}
and a test class for it:
class ValidatorTest {
Validator validator = new ValidatorImpl();
@Test
void validatorShouldThrowIllegalArgumentExceptionIfDividendBelowZero() {
assertThrows(IllegalArgumentException.class, () -> validator.validate(-10, 10),
"Dividend must be above zero");
}
@Test
void validatorShouldThrowIllegalArgumentExceptionIfDivisorBelowZero() {
assertThrows(IllegalArgumentException.class, () -> validator.validate(10, -10),
"Divisor must be above zero");
}
@Test
void validatorShouldThrowIllegalArgumentExceptionIfDivisorEqualsZero() {
assertThrows(IllegalArgumentException.class, () -> validator.validate(10, 0),
"You can`t divide by ZERO!");
}
}
and here is a result of Jacoco, it shows that the last condition is half covered (1 of 2 branches missed), but I cannot understand why it happens and what is a second branch. Test exists and it's passed well. Even if i swap IF
in ValidatorImpl
, Jacoco shows that last IF
is half covered.
Image