Unable to cover the code coverage from if condition inside for loop.

public class cwrapper{
    apply__c g = new apply__c();
    selected = false;

    public cwrapper(apply__c a, boolean selected){
        a = g;
        selected = true;


// for loop
for(cWrapper wrp : cwrapperlist){
    if(wrp.selected ){
        mapRecords.put(wrp.g.id, wrp);
    if(finwrp.selected  == false && mapRecords.containsKey(wrp.g.id)){

How to cover from if condition inside forloop in test class.

  • should your loop be inside the method ?call that method. – unidha May 26 '17 at 9:01
  • its a private method which is called in another method.I have called public method but the coverage has been done till for loop only ,It is not entering for loop,I want whole thing to be covered. – sfdclearner May 26 '17 at 9:05
  • I move comment to answer because the format is better there. – unidha May 26 '17 at 9:14
  • cwrapperlist will be empty thats why for loop is not getting covered. Check from where cwrapperlist is populated. – Reshma May 26 '17 at 9:20

If your method is private, use @TestVisible so it will be reachable by test class.

// Private method
    @TestVisible private static void updateRecord(String name) {
        // Do something

I believe control is not entering the for loop, because your cwrapperlist is empty, please make sure you call the correct code to populate this list before trying to cover the for loop and inside.

  • 1
    Please stop adding hope this helps to every answer. It's implicit in the fact you are posting an answer that you hope it will help. SFSE is not a forum, but rather a terse Q&A site. – Adrian Larson May 26 '17 at 13:45
  • 1
    sure, will take care now onwards! – Nitish Singhal May 26 '17 at 13:48
  • @NitishSinghal My wrapperlist is having all the required values .....checked it with system.assert.but it is not entering the for loop – sfdclearner May 26 '17 at 14:33
  • 1
    @anjali If the line for(cWrapper wrp : cwrapperlist){ is covered but the contents between the braces aren't, it means that Salesforce has looked at the for loop but because the collection is empty, it's got nothing to do so skips it. It does sound like your collection is empty. – Dan Jones May 26 '17 at 14:35
  • @DanJones - when I checked with system.assertequals(null,actual) - test class is failing because actual has values means wrapperlist has values . Is it right? – sfdclearner May 28 '17 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.