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
sona gaikwadsona gaikwad 

how to write test class for this method ---urgent

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

 
Best Answer chosen by sona gaikwad
sona gaikwadsona gaikwad
@isTest 
public with sharing class Checkleaddetails { 
   public static testMethod void checklead() { 
   
          
        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.PersonMobilePhone,     
            Mobile_2__c= c.Mobile_2__c,
            Whats_App_Number__c= c.Whats_App_Number__c,
            LandLine__c= c.LandLine__c,
            Email= c.PersonEmail);
            
          insert leadobj ;
    
        Test.StartTest();      
        Id [] soslSearchResults= new Id[6];
        soslSearchResults[0] = leadobj.Id;
        soslSearchResults[1] = c.Id;
        Test.setFixedSearchResults(soslSearchResults);
        LeadHandlerClass.LeadDuplicateCheck(leadobj.Email);
        LeadHandlerClass.LeadDuplicateCheck(leadobj.MobilePhone);    
        LeadHandlerClass.LeadDuplicateCheck(leadobj.Mobile_2__c);
        LeadHandlerClass.LeadDuplicateCheck(leadobj.Whats_App_Number__c);
        LeadHandlerClass.LeadDuplicateCheck(leadobj.LandLine__c);
        LeadHandlerClass.CCHelp();
        Test.StopTest();
          
 } 
  public static testMethod void checkaccount() { 
   
         
        Account ac = new Account();
     {    
        RecordType personAccountRecordTypeId = [select developerName from RecordType where IsPersonType=True  And DeveloperName = 'personAccount'];
        ac.RecordtypeId = personAccountRecordTypeId.Id;
        ac.PersonMobilePhone='9743800309';
        ac.LastName='Jitesh';
        ac.FirstName='kape';
        ac.Date_of_Birth__c = date.today()-2;
        ac.Mobile_2__c='9743800309';
        ac.Whats_App_Number__c = '8652146259';
        ac.LandLine__c ='022-25412365';
        ac.PersonEmail = 'email@gmail.com';
      
        insert ac;
       } 
              
           
        Account a = new Account();
     {    
        RecordType personAccountRecordTypeId = [select developerName from RecordType where IsPersonType=True  And DeveloperName = 'personAccount'];
        a.RecordtypeId = personAccountRecordTypeId.Id;
        
        a.LastName='Jitesh';
        a.FirstName='shani';
        a.PersonMobilePhone= ac.id;
        a.Date_of_Birth__c = date.today()-2;
        a.Mobile_2__c='9743800309';
        a.Whats_App_Number__c = '8652146259';
        a.LandLine__c ='022-25412365';
        a.PersonEmail = 'email@gmail.com';
      
        insert a;
       } 
        Test.StartTest();      
        Id [] soslSearchResults= new Id[6];
        soslSearchResults[0] = a.Id;
        soslSearchResults[1] = ac.Id;
        Test.setFixedSearchResults(soslSearchResults);
        LeadHandlerClass.LeadDuplicateCheck(a.PersonEmail);
        LeadHandlerClass.LeadDuplicateCheck(a.PersonMobilePhone);    
        LeadHandlerClass.LeadDuplicateCheck(a.Mobile_2__c);
        LeadHandlerClass.LeadDuplicateCheck(a.Whats_App_Number__c);
        LeadHandlerClass.LeadDuplicateCheck(a.LandLine__c);
        LeadHandlerClass.CCHelp();
        Test.StopTest();
          
 }             
}

 

All Answers

Raj VakatiRaj Vakati
Try like this
 
@isTest
private class checkleaddetailsTest {
public static testMethod Boolean checkleadinfo () {

Account c = new Account();
c.RecordTypeId= [select developerName from RecordType where IsPersonType=True  And DeveloperName = 'personAccount'];
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.FirstName,
LastName = c.LastName,
MobilePhone = c.PersonMobilePhone,
Mobile_2__c= c.Mobile_2__c,
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 ;
}
}

 
sona gaikwadsona gaikwad
@isTest 
public with sharing class Checkleaddetails { 
   public static testMethod void checklead() { 
   
          
        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.PersonMobilePhone,     
            Mobile_2__c= c.Mobile_2__c,
            Whats_App_Number__c= c.Whats_App_Number__c,
            LandLine__c= c.LandLine__c,
            Email= c.PersonEmail);
            
          insert leadobj ;
    
        Test.StartTest();      
        Id [] soslSearchResults= new Id[6];
        soslSearchResults[0] = leadobj.Id;
        soslSearchResults[1] = c.Id;
        Test.setFixedSearchResults(soslSearchResults);
        LeadHandlerClass.LeadDuplicateCheck(leadobj.Email);
        LeadHandlerClass.LeadDuplicateCheck(leadobj.MobilePhone);    
        LeadHandlerClass.LeadDuplicateCheck(leadobj.Mobile_2__c);
        LeadHandlerClass.LeadDuplicateCheck(leadobj.Whats_App_Number__c);
        LeadHandlerClass.LeadDuplicateCheck(leadobj.LandLine__c);
        LeadHandlerClass.CCHelp();
        Test.StopTest();
          
 } 
  public static testMethod void checkaccount() { 
   
         
        Account ac = new Account();
     {    
        RecordType personAccountRecordTypeId = [select developerName from RecordType where IsPersonType=True  And DeveloperName = 'personAccount'];
        ac.RecordtypeId = personAccountRecordTypeId.Id;
        ac.PersonMobilePhone='9743800309';
        ac.LastName='Jitesh';
        ac.FirstName='kape';
        ac.Date_of_Birth__c = date.today()-2;
        ac.Mobile_2__c='9743800309';
        ac.Whats_App_Number__c = '8652146259';
        ac.LandLine__c ='022-25412365';
        ac.PersonEmail = 'email@gmail.com';
      
        insert ac;
       } 
              
           
        Account a = new Account();
     {    
        RecordType personAccountRecordTypeId = [select developerName from RecordType where IsPersonType=True  And DeveloperName = 'personAccount'];
        a.RecordtypeId = personAccountRecordTypeId.Id;
        
        a.LastName='Jitesh';
        a.FirstName='shani';
        a.PersonMobilePhone= ac.id;
        a.Date_of_Birth__c = date.today()-2;
        a.Mobile_2__c='9743800309';
        a.Whats_App_Number__c = '8652146259';
        a.LandLine__c ='022-25412365';
        a.PersonEmail = 'email@gmail.com';
      
        insert a;
       } 
        Test.StartTest();      
        Id [] soslSearchResults= new Id[6];
        soslSearchResults[0] = a.Id;
        soslSearchResults[1] = ac.Id;
        Test.setFixedSearchResults(soslSearchResults);
        LeadHandlerClass.LeadDuplicateCheck(a.PersonEmail);
        LeadHandlerClass.LeadDuplicateCheck(a.PersonMobilePhone);    
        LeadHandlerClass.LeadDuplicateCheck(a.Mobile_2__c);
        LeadHandlerClass.LeadDuplicateCheck(a.Whats_App_Number__c);
        LeadHandlerClass.LeadDuplicateCheck(a.LandLine__c);
        LeadHandlerClass.CCHelp();
        Test.StopTest();
          
 }             
}

 
This was selected as the best answer