0
public UtilClass {
    public static Boolean someBool = true;

    public static List<SomeObject> someMethod() {
        .
        .
        if(someBool) {
            // do something
        }

        return someList;
    }
}

@isTest
public TestClass {
    @isTest
    public static void testMe() {
        System.assertEquals(1, UtilClass.someMethod().size());
    }
}

I expected the "if block" inside someMethod() to be covered by test code, but it is not. What could be the possible reason? However, if I initialize the boolean to true explicity before calling the someMethod() in System.assertEquals() the "if block" is covered.

1 Answer 1

1

Not sure how you are running your tests & checking your code coverage,

but when I tried the exact same code in my dev org, i got the coverage for the IF block as expected.. see the screenshot

enter image description here

here's the code I tried

public class StaticString {

    public static Boolean someBool = true;

    public static List<Contact> someMethod() {
        if(someBool) {
            List<Contact> c = new List<Contact>();
            return c;
        }
        return null;
    }
}

Test Class

@isTest
public class TestStaticString {
    @isTest
    public static void testMe() {
        System.assertEquals(1, StaticString.someMethod().size());
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .