0

test class not covered all lines

 public static Boolean LeadDuplicateCheck(String contactInfo){
     String searchQuery= 'FIND \'' + contactInfo.trim() + '*\' IN ALL FIELDS RETURNING Lead(Id,MobilePhone,Mobile_2__c,Whats_App_Number__c,LandLine__c,Email), Account(Id,PersonMobilePhone,Mobile_2__c,Whats_App_Number__c,LandLine__c,PersonEmail)';
     List<List <sObject>> searchList= search.query(searchQuery);
      Lead [] leads= ((List<Lead>)searchList[0]);
      Account [] accounts = ((List<Account>)searchList[1]);


    if(leads.size()>0) 
    { 
       /* this line is not covered */
     for(Lead lead:leads ){
      if((lead.MobilePhone!=null && contactInfo.contains(lead.MobilePhone))||(lead.Mobile_2__c!=null && contactInfo.contains(lead.Mobile_2__c))||(lead.Whats_App_Number__c!=null &&contactInfo.contains(lead.Whats_App_Number__c))||
      (lead.LandLine__c!=null && contactInfo.contains(lead.LandLine__c))||(lead.Email!=null && contactInfo.contains(lead.Email)))
       return true; //duplicate return true
     }
     /* this line is not covered */

    }
     if(accounts.size()>0) 
    {     /* this line is not covered */
     for(Account account:accounts ){
      if((account.PersonMobilePhone!=null && contactInfo.contains(account.PersonMobilePhone))||(account.Mobile_2__c!=null && contactInfo.contains(account.Mobile_2__c))||
      (account.Whats_App_Number__c!=null &&contactInfo.contains(account.Whats_App_Number__c))||
      (account.LandLine__c!=null && contactInfo.contains(account.LandLine__c))||(account.PersonEmail!=null && contactInfo.contains(account.PersonEmail)))
       return true; //duplicate return true
     }  /* this line is not covered */

    }

   return false;

}

**test class**

        @isTest 
    public with sharing class checkleaddetails { 
       public static testMethod Boolean checkleadinfo () { 


            Account c = new Account();
         {    
            RecordType personAccountRecordTypeId = [select developerName from RecordType where IsPersonType=True  And DeveloperName = 'personAccount'];
            c.RecordtypeId = personAccountRecordTypeId.Id;
            c.PersonMobilePhone='9743800309';
            c.LastName='Jitesh';
            c.FirstName='kape';
            c.Date_of_Birth__c = date.today()-2;
            c.Mobile_2__c='9743800309';
            c.Whats_App_Number__c = '8652146259';
            c.LandLine__c ='022-25412365';
            c.PersonEmail = 'email@gmail.com';

            insert c;
           } 


             Lead leadobj=new Lead(
                FirstName=c.id,
                LastName = c.id,        
                MobilePhone = c.id,     
                Mobile_2__c= c.id,
                Whats_App_Number__c= c.Whats_App_Number__c,
                LandLine__c= c.LandLine__c,
                Email= c.PersonEmail);

              insert leadobj ;



            Test.StartTest();   
            LeadHandlerClass.LeadDuplicateCheck(leadobj.MobilePhone);    
            LeadHandlerClass.LeadDuplicateCheck(leadobj.Mobile_2__c);
           LeadHandlerClass.LeadDuplicateCheck(leadobj.Whats_App_Number__c);
           LeadHandlerClass.LeadDuplicateCheck(leadobj.LandLine__c);
           LeadHandlerClass.LeadDuplicateCheck(leadobj.Email);




           LeadHandlerClass.CCHelp();
            Test.StopTest();
               return false ;
     }       
    }
0

You cannot call a static method using the instance of your class. Instead you should call your method using your class name.

Make this three changes in the code

  1. Call your method using class name i.e. ClassName.MethodName('parameter').
  2. Your method accepts string as a parameter, hence pass string to your method in test class.
  3. You have to use a special syntax when testing SOSL

Change your code between Test.startTest() and Test.stopTest() to

Id [] soslSearchResults= new Id[2];
soslSearchResults[0] = leadobj.Id;
soslSearchResults[1] = c.Id;
Test.setFixedSearchResults(soslSearchResults);
LeadHandlerClass.LeadDuplicateCheck(leadobj.Email);  

Note :

  • You are assigning ID value to email field on Lead in test class (Lead creation code in test class) , which will give you error, so you will have to correct that as well.
    Also, I mentioned email just as an example, you can pass any other field on which you want your duplicates to be identified.
  • You also might have to convert your fields which are of different data type to string while passing it as a parameter to your method.
1

Your signature looks like:

public with sharing class MyClass
{
    public static Boolean myMethod(String input)
    {
        // logic
    }
}

Currently you are calling it like:

MyClass instance = new MyClass();
instance.myMethod();

But two things are wrong with that:

  1. You need to call it statically, not from an instance.
  2. You need to pass a String parameter.

So it should look like:

MyClass.myMethod('Some Value');

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.