You need to sign in to do that
Don't have an account?

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;
}
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;
}
All Answers
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