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
Swamy PSwamy P 

Test Class Coverage for If clause

Hi,

     I Wrote a Test Class for a trigger.Thus Trigger related to some conditions.

This is Trigger:

trigger match_territory on Lead (before insert) {

   
    for(lead l:trigger.new)
    {
    if(l.Site__c=='PressKart')
    {
        if(l.country!=null){
              if(l.country=='AntiguaandBarbuda'){
                    l.country='Antigua and Barbuda';
                    l.Territory__c = 'Americas';                
                }

               if(l.country=='Croatia'){
                    l.Territory__c = 'ROW';
               }

       }

    }

  }

}

------------------

Test Class:

@isTest
public class test_presskart {
    static testMethod void testpresskart()
     {
        Map<String,String> cuntr1 = new Map<String,String>();                    
             cuntr1.put('ROW','Croatia');
             cuntr1.put('Americas','Antigua and Barbuda' );

List<Lead> leadlist = new List<Lead>();
    for(string s:cuntr1.Keyset())
         {
         for(string s1:cuntr1.Values())
            {
            
                Lead l = new Lead();
                l.site__c = 'presskart';
                l.country = s1 ;
                l.Company = 'Pressmart';
                l.Status = 'Open';
                l.Org_Type__c = 'ePortal';
                l.LastName = 'TestingLead';
                l.Frequency__c = 'Daily';
                l.Phone = '9998877554';
                l.Email = 'test@test.com';
                l.CurrencyIsoCode = 'INR' ;
                l.Territory__c = s;                
                l.Monthly_Rev__c = 50000.0;               
                
              leadlist.add(l);
           }
         }
         if(leadlist.size()>0)
         {
             insert leadlist ;
         }
    }

}

Once Check it Dudes,Am getting only 45%.I Want to improve it.Can anyone suggest me.

 

SFDC_VikashSFDC_Vikash

Hi,

 

You code also right only the one thing is has to be changed indicated  by orange fonts as you are comparing a string without  spaces in trigger code:

 

Map<String,String> cuntr1 = new Map<String,String>();                    
             cuntr1.put('ROW','Croatia');
             cuntr1.put('Americas','AntiguaandBarbuda' );

 

Also you can try this one :

 

@isTest
public class test_presskart {
    static testMethod void testpresskart() {        
	  List<Lead> leadList = new List<Lead>();
      Lead lead1 = createLead('AntiguaandBarbuda');
	  leadList.add(lead1);
	  Lead lead2 = createLead('Croatia');
	  leadList.add(lead2);
      insert leadList ;      
    }
	
   private Lead createLead(String countryName, String territoryName){
     Lead lead = new Lead();
     lead.site__c = 'presskart';
     lead.country = countryName ;
     lead.Company = 'Pressmart';
     lead.Status = 'Open';
     lead.Org_Type__c = 'ePortal';
     lead.LastName = 'TestingLead';
     lead.Frequency__c = 'Daily';
     lead.Phone = '9998877554';
     lead.Email = 'test@test.com';
     lead.CurrencyIsoCode = 'INR' ;
     lead.Territory__c = territoryName;                
     lead.Monthly_Rev__c = 50000.0; 
   }   
}