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?