0

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

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
  • You most likely need a positive test for the remaining part behind the last `if` (even though there is nothing). – Progman Aug 18 '20 at 20:44

1 Answers1

3

That happens because there are values of divisor and dividend that result in none of the if statements being executed. Create an additional test where you call validate(10,10); and see if that covers the entire method.

JustAnotherDeveloper
  • 2,061
  • 2
  • 10
  • 24