英文:
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("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
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论