Tack
Tack

Reputation: 121

Why is Jacoco flagging my for loop as partially covered?

In a simple method with a for loop and an if statement, Jacoco flags the for loop as partially covered. Why is this?

This issue seems related, but I couldn't figure out how to apply it to my situation: https://github.com/jacoco/jacoco/issues/370

I suspect it has something to do with the lone "if" statement.

MRE:

   public SomeClass test(){
        SomeClass find = new SomeClass();

        ArrayList <String> myArrayList = new ArrayList<>();
        myArrayList.add("A");
        myArrayList.add("B");

        for(String s : myArrayList){
            if(s.equals("B")){break;}
        }

        return find;
    }

A corresponding Test:

@Test
    public void testTest(){
        SomeClass find = new SomeClass();
        find.test();
    }

The example code would show the line with the "for" loop as partially covered, and every other line covered. I expect it to show every line as covered.

Upvotes: 1

Views: 1895

Answers (1)

jtahlborn
jtahlborn

Reputation: 53694

maybe because you don't test the scenario where the value isn't found and the for loop goes to completion. the foreach loop is just syntactic sugar for a for loop with an iterator and a test for iter.hasNext();. in your test case, you only ever see the case where that returns true.

Upvotes: 1

Related Questions