+ Start a Discussion
P KumarP Kumar 

Code Coverage Issue, How do i get 100% Code Coverage?

Hi,
Following is my trigger and test Class, Please help in acheiveing min 75% of Code Coverage.
Now my trigger has only 49% of code coverage.
I have checked under developer console, Bolded text in trigger are not covered by test class.
Need Help ASAP.
========================================================================
@isTest(SeeAllData=true)
public class LeadTaggingTest{
public static testMethod void LeadTaggingStaticTestMethod(){

Id rtID= [select Id,name from RecordType where name ='APAC - Open' limit 1].Id;
Id SiteID= [select Id,name from RecordType where name ='Site' limit 1].Id;
Id BillRtId = [Select Id, name from RecordType where name = 'Billing Account' limit 1].Id;
User O = [select id from user where id='00520000001Omvk' LIMIT 1];
Set<String> allLeadEmails = new Set <String> ();
Set<String> allFirstNames = new Set <String> ();
Set<String> allLastNames = new Set <String> ();
Set<String> allCompanyCodes = new Set <String> ();
Set<String> APACCompanyCodes = new Set<String>{'AU1', 'SG1', 'IN1', 'NZ1', 'CN1'};

try{
    Test.startTest();
  {
      Account Billing = 
      new Account (
          Name='TestBillingAcc', 
          RecordTypeId=BillRtId, 
          Company__c='SG1',
          Primary_Account_Owner__c='Katherine Schuil',
          Primary_Account_Owner_ID__c='TOFFKA',
          Primary_Sales_Function__c='TeleMarketing',
          Owner = O);
      insert Billing;
    
      Account Site = 
      new Account (
          Name='TestSiteAcc', 
          RecordTypeId=SiteId, 
          Company__c='SG1', 
          Ship_To__c = '001',
          Primary_Account_Owner__c='Katherine Schuil',
          Primary_Account_Owner_ID__c='TOFFKA',
          Primary_Sales_Function__c='TeleMarketing',
          Owner = O,
          Parent = Billing,
          Specific_Customer_Type__c='Other',
          CEM_OEM_Tier__c=1,
          Fixed_Quote_Comments__c='Testing Fixed Quote Comments');
      insert Site;

      Contact c = 
      new Contact (
          FirstName = 'Test',
          LastName = 'Lead',
          Email = 'testinglead@element14.com',
          Account_Number__c = '12345',
          PIN__c = '064582',
          First_Order_Date__c = Date.newInstance( 2015, 2, 2 ),
          Turnover__c = 10000.00,
          Account = Site
          );
      insert c;
  
      Lead l = 
      new Lead (
          FirstName ='Test', 
          LastName ='Lead', 
          Country_code__c ='Singapore',
          Company = 'SG1',
          email = 'testinglead@element14.com',
          Lead_Type__c = 'New Company',
          LeadSource = 'Supplier Lead Referral',
      RecordTypeId=rtID,
          Lead_Source_Detail_NEW__c = '3M');
          insert l;

Contact c1 = 
      new Contact (
          FirstName = 'TestP',
          LastName = 'Lead',
          Email = 'testinglead@element14.com',
          Account_Number__c = '12345',
          PIN__c = '064582',
          First_Order_Date__c = Date.newInstance( 2015, 2, 2 ),
          Turnover__c = 0.00,
          Account = Site
          );
insert c1;          

Lead l1 = 
      new Lead (
          FirstName ='Test', 
          LastName ='Lead', 
          Country_code__c ='Singapore',
          Company = 'SG1',
          email = 'testinglead@element14.com',
          Lead_Type__c = 'New Company',
          LeadSource = 'Supplier Lead Referral',
          RecordTypeId=rtID,
          Lead_Source_Detail_NEW__c = '3M');
insert l1;         
 }
 Test.stopTest();
} catch (Exception e) {
    system.assertEquals('********* Error Found ********', e.getMessage());
    System.debug('******@ Exception*****');
}
System.debug('******End of Test*****');
}
}
========================================================================
Trigger LeadTagging on Contact (after insert, after update)
{
  if( ContactLeadTriggerHandler.isLeadTrigger == false )
    {
        ContactLeadTriggerHandler.isContactTrigger = true;
        
              Id rtID= [select Id,name from RecordType where name ='APAC - Open' limit 1].Id;
               Set<String> setEmailId = new Set<String>();
               Set<String> setFirstName = new Set<String>();
               Set<String> setLastName = new Set<String>();

                for(Contact myContact : Trigger.new)
               {
                       if(myContact.Email != null && myContact.FirstName != null && myContact.LastName != null)
                       {
                               setEmailId.add(myContact.Email);
                               setFirstName.add(myContact.FirstName);
                               setLastName.add(myContact.LastName);
                       }
               }
              List<lead> listLead = [ Select Id, FirstName, LastName, Email, Status, Ready_to_Convert__c, Country_code__c FROM Lead where RecordTypeId = :rtId AND ( Email in :setEmailId or FirstName in :setFirstName or LastName in :setLastName ) ] ;
               
               Map<String,Lead> mapKeyWiseLead = new Map<String,Lead>();
               for(Lead obj :  listLead )
               {
                    String key = obj.Email +'-'+obj.FirstName+'-'+obj.LastName;
                    mapKeyWiseLead.put(key,obj);
               }
               
               List<Lead> listUpdatelead = new List<Lead>();
               Set<String> setLeadID = new Set<String>(); // Added to check duplicate Lead
                
                String key;
                for(Contact myContact : Trigger.new)
                {
                    key='';
                    if(myContact.email != null && myContact.FirstName != null && myContact.LastName != null)
                    {
                        key = myContact.Email +'-'+myContact.FirstName+'-'+myContact.LastName;
                        if( mapKeyWiseLead.containsKey(key) )
                        {
                            Lead checkLead = mapKeyWiseLead.get(Key) ;
                            
                               if( checkLead.Email == myContact.email &&  checkLead.FirstName == myContact.FirstName && checkLead.LastName == myContact.LastName )
                                {
                                    System.debug('Lead to Update :'+checkLead.id);
                                      if(myContact.Turnover__c > 0.00)
                                      {
                                              if(setLeadID.contains(checkLead.id) == false)
                                              {
                                                     checkLead.Status = 'Order Placed';
                                                     checkLead.Ready_to_Convert__c = TRUE;
                                                     listUpdatelead.add(checkLead);
                                                     setLeadID.add(checkLead.id);
                                              }
                                      }
                                      if((myContact.Turnover__c <= 0.00 || myContact.Turnover__c== null) && (checkLead.Country_code__c != 'India' || checkLead.Country_code__c != 'China' || checkLead.Country_code__c != 'Taiwan' || checkLead.Country_code__c != 'Hong Kong'))
                                      {
                                              if(setLeadID.contains(checkLead.id) == false)
                                              {
                                                     checkLead.Status = 'Account created';
                                                     listUpdatelead.add(checkLead);
                                                     setLeadID.add(checkLead.id);
                                              }      
                                      }
                                                     checkLead.Existing_Contact__c = TRUE;
                                                     checkLead.Contact_Created_Date__c = myContact.Created_Date__c;
                                                     checkLead.Contact_ID__c = myContact.Id;
                                                     checkLead.Account_Number__c = myContact.Account_number__c;
                                                     checkLead.Ship_To__c = myContact.Ship_To__c;
                                                     checkLead.Contact_Company__c = myContact.Company__c;
                                                     checkLead.Primary_Account_Owner__c = myContact.Primary_Account_Owner__c;
                                                     checkLead.Primary_Account_Owner_ID__c = myContact.Primary_Account_Owner_ID__c;
                                                     checkLead.Contact_Primary_Sales_Function__c = myContact.Primary_Sales_Function__c;
                                                     checkLead.First_Order_Date__c = myContact.First_Order_Date__c;
                                                     checkLead.Contact_Owner__c = myContact.ownerID;
                                                     checkLead.Turnover__c = myContact.Turnover__c;
                                                     checkLead.PIN__c = myContact.PIN__c;
                                }      
                        }
                    }
                }


               if( listUpdatelead.size() > 0 )
               {
                       try
                       {
                               update listUpdatelead;            
                       }
                       catch(DMLException e)
                       {  
                               System.debug('The following exception has occurred: ' + e.getMessage());
                       }
                 }
        }
}

========================================================================
Best Answer chosen by P Kumar
Amit Chaudhary 8Amit Chaudhary 8
Hi Parvin,

Please try below code .
@isTest(SeeAllData=true)
public class LeadTaggingTest
{
	public static testMethod void LeadTaggingStaticTestMethod()
	{
		Test.startTest();

			Id rtID= [select Id,name from RecordType where name ='APAC - Open' limit 1].Id;
		  
			Account Billing = new Account (
				  Name='TestBillingAcc', 
				  RecordTypeId=BillRtId, 
				  Company__c='SG1',
				  Primary_Account_Owner__c='Katherine Schuil',
				  Primary_Account_Owner_ID__c='TOFFKA',
				  Primary_Sales_Function__c='TeleMarketing');
			insert Billing;
			
			Account Site =  new Account (
				  Name='TestSiteAcc', 
				  Company__c='SG1', 
				  Ship_To__c = '001',
				  Primary_Account_Owner__c='Katherine Schuil',
				  Primary_Account_Owner_ID__c='TOFFKA',
				  Primary_Sales_Function__c='TeleMarketing',
				  Parent = Billing,
				  Specific_Customer_Type__c='Other',
				  CEM_OEM_Tier__c=1,
				  Fixed_Quote_Comments__c='Testing Fixed Quote Comments');
			insert Site;

			  Contact c = new Contact (
				  FirstName = 'Test',
				  LastName = 'Lead',
				  Email = 'testinglead@element14.com',
				  Account_Number__c = '12345',
				  PIN__c = '064582',
				  First_Order_Date__c = Date.newInstance( 2015, 2, 2 ),
				  Turnover__c = 10000.00,
				  Accountid = Site.id
				  );
			  insert c;
		  
			Lead l = new Lead (
			  FirstName ='TestP', 
			  LastName ='Lead', 
			  Country_code__c ='Singapore',
			  Company = 'SG1',
			  email = 'testinglead@element14.com',
			  Lead_Type__c = 'New Company',
			  LeadSource = 'Supplier Lead Referral',
			  RecordTypeId=rtID,
			  Lead_Source_Detail_NEW__c = '3M');
			insert l;

			Contact c = new Contact (
			  FirstName = 'Test',
			  LastName = 'Lead',
			  Email = 'testinglead@element14.com',
			  Account_Number__c = '12345',
			  PIN__c = '064582',
			  First_Order_Date__c = Date.newInstance( 2015, 2, 2 ),
			  Turnover__c = 10000.00,
			  Accountid = Site.id
			  );
			insert c;
			
			Contact c1 =  new Contact(
			  FirstName = 'TestP',
			  LastName = 'Lead',
			  Email = 'testinglead@element14.com',
			  Account_Number__c = '12345',
			  PIN__c = '064582',
			  First_Order_Date__c = Date.newInstance( 2015, 2, 2 ),
			  Turnover__c = 0.00,
			  Accountid = Site.id
			  );
			insert c1;          

		Test.stopTest();
	}

}



 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Hi Parvin,

Please try below code .
@isTest(SeeAllData=true)
public class LeadTaggingTest
{
	public static testMethod void LeadTaggingStaticTestMethod()
	{
		Test.startTest();

			Id rtID= [select Id,name from RecordType where name ='APAC - Open' limit 1].Id;
		  
			Account Billing = new Account (
				  Name='TestBillingAcc', 
				  RecordTypeId=BillRtId, 
				  Company__c='SG1',
				  Primary_Account_Owner__c='Katherine Schuil',
				  Primary_Account_Owner_ID__c='TOFFKA',
				  Primary_Sales_Function__c='TeleMarketing');
			insert Billing;
			
			Account Site =  new Account (
				  Name='TestSiteAcc', 
				  Company__c='SG1', 
				  Ship_To__c = '001',
				  Primary_Account_Owner__c='Katherine Schuil',
				  Primary_Account_Owner_ID__c='TOFFKA',
				  Primary_Sales_Function__c='TeleMarketing',
				  Parent = Billing,
				  Specific_Customer_Type__c='Other',
				  CEM_OEM_Tier__c=1,
				  Fixed_Quote_Comments__c='Testing Fixed Quote Comments');
			insert Site;

			  Contact c = new Contact (
				  FirstName = 'Test',
				  LastName = 'Lead',
				  Email = 'testinglead@element14.com',
				  Account_Number__c = '12345',
				  PIN__c = '064582',
				  First_Order_Date__c = Date.newInstance( 2015, 2, 2 ),
				  Turnover__c = 10000.00,
				  Accountid = Site.id
				  );
			  insert c;
		  
			Lead l = new Lead (
			  FirstName ='TestP', 
			  LastName ='Lead', 
			  Country_code__c ='Singapore',
			  Company = 'SG1',
			  email = 'testinglead@element14.com',
			  Lead_Type__c = 'New Company',
			  LeadSource = 'Supplier Lead Referral',
			  RecordTypeId=rtID,
			  Lead_Source_Detail_NEW__c = '3M');
			insert l;

			Contact c = new Contact (
			  FirstName = 'Test',
			  LastName = 'Lead',
			  Email = 'testinglead@element14.com',
			  Account_Number__c = '12345',
			  PIN__c = '064582',
			  First_Order_Date__c = Date.newInstance( 2015, 2, 2 ),
			  Turnover__c = 10000.00,
			  Accountid = Site.id
			  );
			insert c;
			
			Contact c1 =  new Contact(
			  FirstName = 'TestP',
			  LastName = 'Lead',
			  Email = 'testinglead@element14.com',
			  Account_Number__c = '12345',
			  PIN__c = '064582',
			  First_Order_Date__c = Date.newInstance( 2015, 2, 2 ),
			  Turnover__c = 0.00,
			  Accountid = Site.id
			  );
			insert c1;          

		Test.stopTest();
	}

}



 
This was selected as the best answer
Eswar Prasad@Sfdc11Eswar Prasad@Sfdc11
hi kumar,
Modified your test class just check it how much getting code coverage.

@isTest(SeeAllData=true)
public class LeadTaggingTest{
public static testMethod void LeadTaggingStaticTestMethod(){

Id rtID= [select Id,name from RecordType where name ='APAC - Open' limit 1].Id;
Id SiteID= [select Id,name from RecordType where name ='Site' limit 1].Id;
Id BillRtId = [Select Id, name from RecordType where name = 'Billing Account' limit 1].Id;
User O = [select id from user where id='00520000001Omvk' LIMIT 1];
Set<String> allLeadEmails = new Set <String> ();
Set<String> allFirstNames = new Set <String> ();
Set<String> allLastNames = new Set <String> ();
Set<String> allCompanyCodes = new Set <String> ();
Set<String> APACCompanyCodes = new Set<String>{'AU1', 'SG1', 'IN1', 'NZ1', 'CN1'};

try{
    Test.startTest();
  {
      Account Billing = 
      new Account (
          Name='TestBillingAcc', 
          RecordTypeId=BillRtId, 
          Company__c='SG1',
          Primary_Account_Owner__c='Katherine Schuil',
          Primary_Account_Owner_ID__c='TOFFKA',
          Primary_Sales_Function__c='TeleMarketing',
          Owner = O);
      insert Billing;
    
      Account Site = 
      new Account (
          Name='TestSiteAcc', 
          RecordTypeId=SiteId, 
          Company__c='SG1', 
          Ship_To__c = '001',
          Primary_Account_Owner__c='Katherine Schuil',
          Primary_Account_Owner_ID__c='TOFFKA',
          Primary_Sales_Function__c='TeleMarketing',
          Owner = O,
          Parent = Billing,
          Specific_Customer_Type__c='Other',
          CEM_OEM_Tier__c=1,
          Fixed_Quote_Comments__c='Testing Fixed Quote Comments');
      insert Site;

      Contact c = 
      new Contact (
          FirstName = 'Test',
          LastName = 'Lead',
          Email = 'testinglead@element14.com',
          Account_Number__c = '12345',
          PIN__c = '064582',
          First_Order_Date__c = Date.newInstance( 2015, 2, 2 ),
          Turnover__c = 10000.00,
          Account = Site
          );
      insert c;
  
      Lead l = 
      new Lead (
          FirstName ='Test', 
          LastName ='Lead', 
          Country_code__c ='Singapore',
          Company = 'SG1',
          email = 'testinglead@element14.com',
          Lead_Type__c = 'New Company',
          LeadSource = 'Supplier Lead Referral',
      RecordTypeId=rtID,
          Lead_Source_Detail_NEW__c = '3M');
          insert l;
Contact mycontact = 
      new Contact (
          FirstName = 'TestP',
          LastName = 'Lead',
          Email = 'testinglead@element14.com',
          Account_Number__c = '12345',
          PIN__c = '064582',
          First_Order_Date__c = Date.newInstance( 2015, 2, 2 ),
          Turnover__c = 0.00,
          Account = Site
          );
insert mycontact;          

Lead checklead = 
      new Lead (
          FirstName ='Test', 
          LastName ='Lead', 
          Country_code__c ='Singapore',
          Company = 'SG1',
          email = 'testinglead@element14.com',
          Lead_Type__c = 'New Company',
          LeadSource = 'Supplier Lead Referral',
          RecordTypeId=rtID,
          Lead_Source_Detail_NEW__c = '3M');
insert checklead;         

checkLead.Existing_Contact__c = TRUE;
                                                     checkLead.Contact_Created_Date__c = myContact.Created_Date__c;
                                                     checkLead.Contact_ID__c = myContact.Id;
                                                     checkLead.Account_Number__c = myContact.Account_number__c;
                                                     checkLead.Ship_To__c = myContact.Ship_To__c;
                                                     checkLead.Contact_Company__c = myContact.Company__c;
                                                     checkLead.Primary_Account_Owner__c = myContact.Primary_Account_Owner__c;
                                                     checkLead.Primary_Account_Owner_ID__c = myContact.Primary_Account_Owner_ID__c;
                                                     checkLead.Contact_Primary_Sales_Function__c = myContact.Primary_Sales_Function__c;
                                                     checkLead.First_Order_Date__c = myContact.First_Order_Date__c;
                                                     checkLead.Contact_Owner__c = myContact.ownerID;
                                                     checkLead.Turnover__c = myContact.Turnover__c;
                                                     checkLead.PIN__c = myContact.PIN__c;

       
 }
 Test.stopTest();
} catch (Exception e) {
    system.assertEquals('********* Error Found ********', e.getMessage());
    System.debug('******@ Exception*****');
}
System.debug('******End of Test*****');
}
}