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
AsaktiAsakti 

test class for before insert event

Hi All i have written a test class for one trigger ,but it does cover all the lines and failing

trigger:
trigger LeadRegionUpdate on Lead (before insert) {
    
    for (Lead L: Trigger.new)
    {
        if(L.Country__c == 'India'){
            L.Lead_Region__c = 'India';
            
        }
        else if(L.Country__c == 'Japan' || L.Country__c == 'China' || L.Country__c == 'Korea')
        {
            L.Lead_Region__c = 'Asia';
            
        }
        else if(L.Country__c == 'Australia' || L.Country__c == 'New Zealand')
        {
            L.Lead_Region__c = 'ANZ';
            
        }
        else if(L.Country__c == 'Canada' || L.Country__c == 'United States of America')
        {
            L.Lead_Region__c = 'North America';
            
        }
        else if(L.Country__c == 'Argentina' || L.Country__c == 'Mexico'
                || L.Country__c == 'Bolivia' || L.Country__c == 'Venezuela'
                || L.Country__c == 'Brazil' || L.Country__c == 'Uruguay' 
                || L.Country__c == 'Guyana' || L.Country__c == 'Suriname' 
                || L.Country__c == 'Chile' || L.Country__c == 'Peru' 
                || L.Country__c == 'Colombia' || L.Country__c == 'Paraguay'
                || L.Country__c == 'Ecuador' || L.Country__c == 'Guyana')
        {
            L.Lead_Region__c = 'South America';
            
        }
        
        else if(L.Country__c == 'Brunei' || L.Country__c == 'Philippines'
                || L.Country__c == 'Burma(Myanmar)' || L.Country__c == 'Singapore'
                || L.Country__c == 'Timor-Leste' || L.Country__c == 'Thailand' 
                || L.Country__c == 'Indonesia' || L.Country__c == 'Suriname' 
                || L.Country__c == 'Taiwan' || L.Country__c == 'Vietnam' 
                || L.Country__c == 'Laos' || L.Country__c == 'Malaysia'
               )
        {
            L.Lead_Region__c = 'South East Asia';
        }
        
        else if(L.Country__c == 'Albania' || L.Country__c == 'Andorra'
                || L.Country__c == 'Armenia' || L.Country__c == 'Austria'
                || L.Country__c == 'Iceland' || L.Country__c == 'Ireland' 
                || L.Country__c == 'Italy' || L.Country__c == 'Kazakhstan' 
                || L.Country__c == 'Latvia' || L.Country__c == 'Liechtenstein' 
                || L.Country__c == 'Hungary' || L.Country__c == 'Greece' 
                || L.Country__c == 'Georgia' || L.Country__c == 'Germany' 
                || L.Country__c == 'Finland' || L.Country__c == 'France'
                || L.Country__c == 'Brunei' || L.Country__c == 'Philippines'
                || L.Country__c == 'Estonia' || L.Country__c == 'Denmark'
                || L.Country__c == 'CzechRepublic' || L.Country__c == 'Cyprus' 
                || L.Country__c == 'Croatia' || L.Country__c == 'Bulgaria' 
                || L.Country__c == 'BosniaandHerzegovina' || L.Country__c == 'Belgium' 
                || L.Country__c == 'Azerbaijan' || L.Country__c == 'Belarus'
               )
        {
            L.Lead_Region__c = 'Europe';
            
        }  
            else if(L.Country__c == 'Algeria' || L.Country__c == 'Bahrain'
                    || L.Country__c == 'Djibouti' || L.Country__c == 'Egypt'
                    || L.Country__c == 'Iran' || L.Country__c == 'Iraq' 
                    || L.Country__c == 'Jordan' || L.Country__c == 'Israel' 
                    || L.Country__c == 'Kuwait' || L.Country__c == 'Lebanon' 
                    || L.Country__c == 'Libya' || L.Country__c == 'Malta' 
                    || L.Country__c == 'Oman' || L.Country__c == 'Qatar' 
                    || L.Country__c == 'Saudi Arabia' || L.Country__c == 'Syria'
                    || L.Country__c == 'Tunisia' || L.Country__c == 'United Arab Emirates'
                    || L.Country__c == 'Palestine' || L.Country__c == 'Yemen'
                    
                   )
            {
                L.Lead_Region__c = 'MENA';
                
            }
        }
    }


Test class:

@isTest
private class TestLeadRegionUpdate {
    
    Private static testMethod void contactDmlTest() {
        
        Test.startTest();
        Lead l = new Lead(lastname='A', company='11',Lead_Type__c= 'Junk',Lead_Region__c = 'India',Country__c = 'India');
        insert l;
                
        Test.stopTest();
         Test.startTest();
        Lead l1 = new Lead(lastname='B', company='11',Lead_Type__c= 'Junk',Lead_Region__c = 'Japan',Country__c = 'Asia');
        insert l1;
                
        Test.stopTest();
         Test.startTest();
        Lead l2 = new Lead(lastname='C', company='11',Lead_Type__c= 'Junk',Lead_Region__c = 'China',Country__c = 'Asia');
        insert l2;
                
        Test.stopTest();
         Test.stopTest();
         Test.startTest();
        Lead l3 = new Lead(lastname='C', company='11',Lead_Type__c= 'Junk',Lead_Region__c = 'Korea',Country__c = 'Asia');
        insert l3;
                
        Test.stopTest();
    }
}

code coverage is 7%
can someone help me with the test class
Best Answer chosen by Asakti
Suraj Tripathi 47Suraj Tripathi 47
Hi,

This is because you haven't provided the  lead.Country__c = 'India' and  lead.Country__c = 'Asia'.
You haven't satisfied the conditions:-
1. L.Country__c == 'United States of America'
2. L.Country__c == 'Japan' ;
3. L.Country__c == 'Australia';
4. L.Country__c == 'Algeria' ;
5.  L.Country__c == 'Austria';
And there are many more else if conditions make them true then your test class will work fine.

Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripath
 

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi,

This is because you haven't provided the  lead.Country__c = 'India' and  lead.Country__c = 'Asia'.
You haven't satisfied the conditions:-
1. L.Country__c == 'United States of America'
2. L.Country__c == 'Japan' ;
3. L.Country__c == 'Australia';
4. L.Country__c == 'Algeria' ;
5.  L.Country__c == 'Austria';
And there are many more else if conditions make them true then your test class will work fine.

Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripath
 
This was selected as the best answer
CharuDuttCharuDutt
Hii Asakti
Try Below Code
 
Mistake In Your Code ===> Lead l1 = new Lead(lastname='B', company='11',Lead_Type__c= 'Junk',Lead_Region__c = 'Japan',Country__c = 'Asia');

Correction ===>  Lead l1 = new Lead(lastname='B', company='11',Lead_Type__c= 'Junk',Country__c = 'Japan',Lead_Region__c= 'Asia');



@isTest
private class TestLeadRegionUpdate {
    
    Private static testMethod void contactDmlTest() {
        
        Test.startTest();
        Lead l = new Lead(lastname='A', company='11',Lead_Type__c= 'Junk',Country__c = 'India',Lead_Region__c = 'India');
        insert l;

        Lead l1 = new Lead(lastname='B', company='11',Lead_Type__c= 'Junk',Country__c= 'Japan',Lead_Region__c  = 'Asia');
        insert l1;
                
        
        Lead l2 = new Lead(lastname='C', company='11',Lead_Type__c= 'Junk', Country__c= 'Australia',Lead_Region__c = 'ANZ');
        insert l2;
                
       
         
        Lead l3 = new Lead(lastname='C', company='11',Lead_Type__c= 'Junk',Country__c = 'Canada',Lead_Region__c = 'North America');
        insert l3;

        Lead l4 = new Lead(lastname='C', company='11',Lead_Type__c= 'Junk',Country__c = 'Ecuador',Lead_Region__c = 'South America');
        insert l4;

        Lead l5 = new Lead(lastname='C', company='11',Lead_Type__c= 'Junk',Country__c = 'Laos',Lead_Region__c = 'South East Asia');
        insert l5;

         Lead l6 = new Lead(lastname='C', company='11',Lead_Type__c= 'Junk',Country__c = 'Azerbaijan',Lead_Region__c = 'Europe');
        insert l6;

         Lead l7 = new Lead(lastname='C', company='11',Lead_Type__c= 'Junk',Country__c = 'Palestine',Lead_Region__c = 'MENA');
        insert l7;
                
        Test.stopTest();
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

 
AsaktiAsakti
Thanks a lot.