function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BroncoBoyBroncoBoy 

How to write tests for if statements in my test class

Question: The developer console indicates that  I need to test every if statment in my for loop below.  Can you suggest how to do that?  Thanks in advance for your help!

Code"

@RemoteAction
    global static list <Marketing_Material__c> retrieveAllMaterialsList(String userDivision)
{
    List<String> imgAudience = new List<String> {'AMC', 'WMC'};
    List<String> jnldAudience = new List<String> {'AMC', 'WMC'};
    List<String> jnlAudience = new List<String> {'AMC', 'WMC'};
    List<String> jnldgAudience = new List<String> {'AMC', 'WMC'};
    List<String> rbdAudience = new List<String> {'AMC', 'WMC'};
    List<String> cclAudience = new List<String> {'CURIAN', 'CURIAN RIA', 'CURIAN SELECT'};
    List<String> siiAudience = new List<String> {'NPH'};
    List<String> npcAudience = new List<String> {'NPH'};
    List<String> icaAudience = new List<String> {'NPH'};
    List<String> investAudience = new List<String> {'NPH'};
    List<String> wAudience = new List<String> {'NPH'};
    Map<String, List<String>> audienceMap = new Map<String, List<String>>();
   
    audienceMap.put('IMG', imgAudience);
    audienceMap.put('JNLD', jnldAudience);
    audienceMap.put('JNL', jnlAudience);
    audienceMap.put('JNLDG', jnldgAudience);
    audienceMap.put('RBD', rbdAudience);
    audienceMap.put('CCL', cclAudience);
    audienceMap.put('SII', siiAudience);
    audienceMap.put('NPC', npcAudience);
    audienceMap.put('ICA', icaAudience);
    audienceMap.put('INVEST', investAudience);
    audienceMap.put('W1', wAudience);

     List <String> finalAudienceListB = new List <String> (audienceMap.get(userDivision)); //e.g. 'WMC, AMC'
     audienceMap.clear();

     Map <Id, Marketing_Material__c> materialListC = new Map <Id, Marketing_Material__c> ();
     List <Marketing_Material__c> finalMaterialList = new List <Marketing_Material__c> ();
     try
     {
         for (Marketing_Material__c mmmm: [SELECT Name, Marketing_Material_Name__c, Form_Number__c, PieceLink__c, PictureLink__c, Approved_Firms__c, Not_Approved_Firms__c, Approved_States__c, Not_Approved_States__c, Item_Cost__c, Audience__c, Product_Line__c, Audience_Groups_Multi_picklist__c, IsObsoleted__c FROM Marketing_Material__c WHERE IsObsoleted__c = false AND(Viewabillity__c = 'Viewable And Orderable' OR Viewabillity__c = 'Viewable Not Orderable')])
         {
             String audC = mmmm.Audience_Groups_Multi_picklist__c; //e.g. 'WMC, AMC, SSU'
             if (!String.isBlank(audC))
             {
                 Integer audienceMatchesC = 0;
                 for (String tt: finalAudienceListB)
                 {
                     //you're finding a matching in the Audience_Groups_Multi_picklist__c(aud) with each item the user is qualified to see based on the user division in the
                     //above hard-coded map.
                     audienceMatchesC = audienceMatchesC + audC.countMatches(tt);
                 }
                 if (audienceMatchesC > 0) //if there's a match then we're good to return the record for the user to see
                 {
                     materialListC.put(mmmm.Id, mmmm); //we add to a map which, by default, prevents duplicates from being added;
                 }
             }
         }
         finalMaterialList = materialListC.values();
     }
     catch (Exception e)
     {
         ApexPages.addMessages(e);
     }
     return finalMaterialList;
}
Best Answer chosen by BroncoBoy
Deepak Kumar ShyoranDeepak Kumar Shyoran
There is no other way or any other standard to write a test class for if condition or etc. rather you have to create some scenario in your test class to meet or pass those if condition in same way as Ravikant mentioned you need to create some Test Data in you test class and need to create data in Test class and then write your Testing code (Test class code ) inside Test.startTest and Test.stopTest.

All Answers

Ravikant kediaRavikant kedia
According to me in your code String audC value is null or blank so your if condition is false so you have to insert one record for Marketing_Material__c with each required field and Audience_Groups_Multi_picklist__c field and this record  also satisfy your condition like sObsoleted__c = false before to call this method.
Deepak Kumar ShyoranDeepak Kumar Shyoran
There is no other way or any other standard to write a test class for if condition or etc. rather you have to create some scenario in your test class to meet or pass those if condition in same way as Ravikant mentioned you need to create some Test Data in you test class and need to create data in Test class and then write your Testing code (Test class code ) inside Test.startTest and Test.stopTest.
This was selected as the best answer
Pratik_MPratik_M
Hi,

As per Salesforcee best practices, for testing the ocnditional statements you should include the sample data which will be pass the test as well as fails (which will check the True and False condition) so all the conditional statements will be covered.

Thanks,
Pratik
BroncoBoyBroncoBoy
Thank you each for your response.  I will try each and mark the best answer.