I have a simple class called SomeClass
:
public class SomeClass {
public int value = 0;
public void inc() {
value++;
}
public void dec()
{
if (isBiggerThanFive()) value--;
value--;
}
private boolean isBiggerThanFive() {
return value > 5;
}
}
And a test class called TheTest
:
class TheTest {
SomeClass t;
@BeforeEach
void setUp() {
t = new SomeClass();
}
@Test
public void whenNewTestIsCreated_ValueIsZero()
{
assertEquals(t.value, 0);
}
@Test
public void whenIncIsCalledWithValueOfZero_ValueIsOne()
{
t.inc();
assertEquals(t.value, 1);
}
@Test
public void whenDecIsCalledWithValueOfZero_ValueIsNegativeOne()
{
t.dec();
assertEquals(t.value, -1);
}
}
Note how the function dec
is structured and how the whole if condition and the statement are on the same line. In the test, this part of decrementing by 2 when the value is bigger that 5 isn't tested. So I assume that I should see in the coverage report that this part is not covered. But that's what I get:
When instead I let the statment be on it's own line, I get the correct results:
Even though the code is completely the same, I get different results based on just the structure of the code. How can I have an indication that a part of a line isn't covered when it's not acutally covered?
IntelliJ's test coverage runner has different run settings (for performance reasons). By default, the Sampling
mode is used. This results in a code line coverage with negligible (execution) slow-down.
Now, if you'd like to have an accurate collection of the branch coverage (such as if-statements) then you can use the
Trace
mode.
Using the
Trace
mode you'll get the following test coverage.
This is further documented on IntelliJ's help website. See Configure code coverage options.