Incorrect code coverage when switch with enum is involved
已回答
I always use "Run SomeTest with Code Coverage" and noticed today some weird results when the source code uses a switch statement with an enum.
The class to test:
public class Calculator {
public int calc(int op, int i1, int i2) {
switch (op) {
case 0:
return i1 * i2;
default:
throw new UnsupportedOperationException("Unknown operation " + op);
}
}
}
And the test class:
public class CalculatorTest {
private final Calculator calculator = new Calculator();
private final int i1 = 5;
private final int i2 = 7;
@Test
public void calc0() {
assertThat(calculator.calc(0, i1, i2)).isEqualTo(35);
}
@Test
public void calc1() {
assertThatThrownBy(() -> calculator.calc(1, i1, i2))
.isInstanceOf(UnsupportedOperationException.class);
}
}
If I run this class with code coverage, I get the expected result:
Now let's change Calculator class using an Operation enum:
public enum Operation {
ADD, SUBTRACT, MULTIPLY, DIVIDE
}
public class Calculator {
public int calc(Operation op, int i1, int i2) {
switch (op) {
case MULTIPLY:
return i1 * i2;
default:
throw new UnsupportedOperationException("Unknown operation " + op);
}
}
}
And the updated test class:
public class CalculatorTest {
private final Calculator calculator = new Calculator();
private final int i1 = 5;
private final int i2 = 7;
@Test
public void calc0() {
assertThat(calculator.calc(Operation.MULTIPLY, i1, i2)).isEqualTo(35);
}
@Test
public void calc1() {
assertThatThrownBy(() -> calculator.calc(Operation.ADD, i1, i2))
.isInstanceOf(UnsupportedOperationException.class);
}
}
But if I now run the test class, the test coverage report shows some strange results:
I tried both JaCoCo as IntelliJ code coverage runners with the same result.
请先登录再写评论。
Hello M Esteban Gutierrez,
Please follow the issue created for your case:
https://youtrack.jetbrains.com/issue/IDEA-294717/Incorrect-code-coverage-when-switch-with-enum-is-involved
Thank you
Hello,
Please tell if the same issue is reproduced for you with the latest IntelliJ IDEA version? (https://www.jetbrains.com/idea/download/#section=mac)
Please also clarify what versions of frameworks are used.
Thank you
I am already using the latest version (Version: 2020.2.1 Build: 202.6948.69), no IDE or plugin updates available.
I created a new simple Java project with only 2 dependencies to write the test class (AssertJ & JUnit). And the results seem to be different from what I noticed yesterday: using IntelliJ code coverage runner gives the expected 100% code coverage when using enum in switch; but with JaCoCo code coverage runner I still get the strange code coverage reports (as mentioned in my previous post).
In the original (spring boot) project I notice exactly the same behavior as in the simple Java project. So ok with IntelliJ code coverage runner, but nok with JaCoCo one.
hysterio,
If possible can you please share the sample project by attaching it to the issue created at YouTrack:
https://youtrack.jetbrains.com/
Or you may upload it here: http://uploads.services.jetbrains.com/
Thank you
Upload id: 2020_09_04_L1tfV3Wm5ZobxbsX (file: calculator.zip)
Hello,
The issue looks like Jacoco specific. Please see the related discussion:
https://github.com/jacoco/jacoco/issues/660
Please contact the https://github.com/jacoco/jacoco
Olga, JaCoCo has resolved the issue in the reporting phase. That is, the synthetic class is instrumented and the statistics tracked. But then, when post-processing the results in order to show them to the user, they are filtered out. Thus, if I build the project with Maven/Gradle and check out the HTML report, I see the results I expect. And this was released in version 0.8.2.
Therefore, the problem is now within IntelliJ, which does not carry out the same filtering when showing the coverage results.
Great job! Looking forward to the 2022.3 release!