Jacoco存在的问题

huangapple go评论74阅读模式
英文:

Problems with Jacoco

问题

这是您提供的翻译:

我正在制作一个除法应用程序,其中包含一个验证器类。在我编写了一些测试时,Jacoco显示了一行代码,表示该代码未被测试覆盖,但实际上已经覆盖了。

验证器的代码:

public class ValidatorImpl implements Validator {

    @Override
    public void validate(int dividend, int divisor) {

        if (divisor == 0) {
            throw new IllegalArgumentException("您不能除以零!");
        }
        if (divisor < 0) {
            throw new IllegalArgumentException("除数必须大于零");
        }
        if (dividend < 0) {
            throw new IllegalArgumentException("被除数必须大于零");
        }
    }
}

以及它的测试类:

class ValidatorTest {

    Validator validator = new ValidatorImpl();

    @Test
    void validatorShouldThrowIllegalArgumentExceptionIfDividendBelowZero() {
        assertThrows(IllegalArgumentException.class, () -> validator.validate(-10, 10),
                "被除数必须大于零");
    }

    @Test
    void validatorShouldThrowIllegalArgumentExceptionIfDivisorBelowZero() {
        assertThrows(IllegalArgumentException.class, () -> validator.validate(10, -10),
                "除数必须大于零");
    }

    @Test
    void validatorShouldThrowIllegalArgumentExceptionIfDivisorEqualsZero() {
        assertThrows(IllegalArgumentException.class, () -> validator.validate(10, 0),
                "您不能除以零!");
    }
}

这是Jacoco的结果,它显示最后一个条件被一半覆盖(2个分支中有1个分支未被覆盖),但我无法理解为什么会发生这种情况,以及第二个分支是什么。测试存在并且通过得很好。即使我交换ValidatorImpl中的IF语句,Jacoco也显示最后的IF语句被一半覆盖。
图片

英文:

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(&quot;You can`t divide by ZERO!&quot;);
        }
        if (divisor &lt; 0) {
            throw new IllegalArgumentException(&quot;Divisor must be above zero&quot;);
        }
        if (dividend &lt; 0) {
            throw new IllegalArgumentException(&quot;Dividend must be above zero&quot;);
        }
    }
}

and a test class for it:

class ValidatorTest {

    Validator validator = new ValidatorImpl();

    @Test
    void validatorShouldThrowIllegalArgumentExceptionIfDividendBelowZero() {
        assertThrows(IllegalArgumentException.class, () -&gt; validator.validate(-10, 10),
                &quot;Dividend must be above zero&quot;);
    }

    @Test
    void validatorShouldThrowIllegalArgumentExceptionIfDivisorBelowZero() {
        assertThrows(IllegalArgumentException.class, () -&gt; validator.validate(10, -10),
                &quot;Divisor must be above zero&quot;);
    }

    @Test
    void validatorShouldThrowIllegalArgumentExceptionIfDivisorEqualsZero() {
        assertThrows(IllegalArgumentException.class, () -&gt; validator.validate(10, 0),
                &quot;You can`t divide by ZERO!&quot;);
    }
}

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

答案1

得分: 3

这是因为存在除数和被除数的值,导致没有任何if语句被执行。创建一个额外的测试,其中调用validate(10,10);,看看是否覆盖了整个方法。

英文:

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.

huangapple
  • 本文由 发表于 2020年8月19日 04:38:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63476283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定