Search code examples
maventestingjunitcode-coveragecobertura

What is the color and number beside the line number in cobertura report


I used mvn cobertura:cobertura to generate this cobertura JUnit test coverage report. Can anyone explain to me what do the numbers beside the line number mean? Thank you.

enter image description here


Solution

  • Those numbers correspond to how many times that line was executed during your tests. Using a simple example:

    public class MyClass {
        public void methodA(){
            System.out.println("Method a");
        }
    
        public void methodB(){
            System.out.println("Method b");
        }
    }
    

    With some tests:

    public class MyClassTest {
    
        @Test
        public void testMethodA(){
            final MyClass x = new MyClass();
            x.methodA();
        }
    
        @Test
        public void testMethodB(){
            final MyClass x = new MyClass();
            x.methodB();
        }
    }
    

    I will get the following report, showing that I constructed my test object twice, and ran each method once: Non ignored test case

    If I add an @Ignore annotation on testMethodB, the following report is produced instead, showing that I only constructed my class once, and did not execute lines within methodB when testing: enter image description here

    The color is associated with coverage. It will appear red when there is no test that covers that line or branch.

    Edit - Regarding your question in the comments, its possible to be missing coverage due to not checking all conditions. For example, consider this method:

    public void methodB(final boolean testOne, final boolean testTwo){
        if(testOne || testTwo){
            System.out.println("Method b");
        }
        System.out.println("Done");
    }
    

    and this test:

    @Test
        public void testMethodB(){
            final MyClass x = new MyClass();
            x.methodB(true, false);
            x.methodB(true, true);
        }
    

    you will end up with the following test report. The reason for this is because although you did execute this line in the test(2 times, in fact), I did not test all permutations of my conditional, therefore, the report will show that I am missing coverage.

    conditional coverage