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
Sana123Sana123 

how to write test class for bulk use cases for this trigger helper

public static void createContactsv2 (List<Account>listAccounts, Map<Id, Account>mapOfoldAccount){
        
        Set<Id> accountIds = new Set<Id>();
        Set<String> contactEmailStringSet = new  Set<String>();
        Set<Account> accIDs = new Set<Account>();
 
        for (Account acc: listAccounts){
            accIDs.add(acc);
        }
    
        List<Contact> listOfdeletContact = [SELECT AccountId FROM Contact WHERE AccountId IN : accIDs];
        if(listOfdeletContact.Size()>0){
           delete listOfdeletContact;

        }
        
        for(Account account : listAccounts){
            system.debug(mapOfoldAccount);
            if(mapOfoldAccount!= null && (String.isNotBlank(account.Contacts__c) && mapOfoldAccount.get(account.Id).Contacts__c != account.Contacts__c))
            {
                
                List<String> listOfContacts = account.Contacts__c.split('\n');
                Set<String> setOfConatcts = new Set<String>(listOfContacts ) ;
                
                for(String lisofstr  : setOfConatcts){
                    System.debug('contactToBeUpsert');
                    
                    accountIds.add(account.Id);
                    contactEmailStringSet.add(lisofstr.substringAfter('Email=').substringBefore(',').replaceAll('\\s', ''));
                    
                }
            }
        }
        
        Map<String, Contact> mapOfContacts = new Map<String, Contact>();
        System.debug(contactEmailStringSet);
        if(contactEmailStringSet.size()>0)
        {     
            for(Contact contact :  [Select id, FirstName, LastName, Email, MobilePhone FROM Contact WHERE AccountId IN :accountIds AND Email IN :contactEmailStringSet]){  
                System.debug(contact);
                mapOfContacts.put(contact.Email, contact);
                
            }
            
        }
         
        List<Contact> contactToBeUpsert = new List<Contact>();
        
        for(Account account : listAccounts)
        {
            if(String.isNotBlank(account.Contacts__c))
            {
                List<String> listOfContactString = account.Contacts__c.split('\n');
                for(String lstString : listOfContactString)
                {  
                    String email = lstString.substringAfter('Email=').substringBefore(',').replaceAll('\\s', '');
                    System.debug(email);
                    if(mapOfContacts.containsKey(email))
                    {
                        contactToBeUpsert.add(new Contact(Id = mapOfContacts.get(email).Id,
                                                          FirstName = lstString.substringAfter('FirstName=').substringBefore(','), 
                                                          LastName = lstString.substringAfter('LastName=').substringBefore(','),
                                                          MobilePhone =lstString.substringAfter('MobilePhone=').substringBefore(',')));
                        System.debug('contactToBeUpsert');  
                    }
                    else
                    {
                        contactToBeUpsert.add(new Contact(FirstName = lstString.substringAfter('FirstName=').substringBefore(','), 
                                                          LastName = lstString.substringAfter('LastName=').substringBefore(','),
                                                          Email = lstString.substringAfter('Email=').substringBefore(','),
                                                          MobilePhone =lstString.substringAfter('MobilePhone=').substringBefore(','),
                                                          AccountId = account.Id));
                        
                        System.debug('contactToBeUpsert');    
                    }  
                }
            }      
        }
     
        System.debug(contactToBeUpsert);
    
            if(contactToBeUpsert.Size()>0){
            upsert contactToBeUpsert;
        }
        
    }
    
    
public static void createContactsv1 (List<Account> listOfAccounts, Map<Id, Account> mapoldAccount){
    
    Set<Id> setOfIds = new Set<Id>();
    Map<Id, List<String>> mapOfAccountIdAndString = new  Map<Id, List<String>>();
   
    List<Account> accountToUpdate = new List<Account>();
    for(Account account : listOfAccounts){
        
        if(String.isNotBlank(account.Move_To_Account__c) && String.isNotBlank(account.Move_To_Contacts__c) && (mapoldAccount == null 
                                               || mapoldAccount.get(account.Id).Move_To_Account__c != account.Move_To_Account__c
                                               || mapoldAccount.get(account.Id).Move_To_Contacts__c != account.Move_To_Contacts__c))
            
        {
             
             List<String> listOfContacts = account.Contacts__c.split('\n');
             List<String> listOfMoveToContacts = account.Move_To_Contacts__c.split('\n');
            
            
            if(listOfContacts.size()>0)
            {
                String moveToContact = '';
                for(String contact : listOfMoveToContacts)
                {
                    if(listOfMoveToContacts.size()>0 && listOfContacts.contains(contact))  {
                        
                        if(!mapOfAccountIdAndString.containsKey(account.Move_To_Account__c)) //false
                            mapOfAccountIdAndString.put(account.Move_To_Account__c, new List<String>());
                        
                        mapOfAccountIdAndString.get(account.Move_To_Account__c).add(contact); //st/ st2 //st3
                        setOfIds.add(account.Id);
                        moveToContact = '\n';//if move to contact value is contains in contact__c value 
                    }
                    else{
                         moveToContact = moveToContact+contact+'\n'; //if not contains then add contact to movettocontact
                    }
                   
                }
                moveToContact = String.isNotBlank(moveToContact)?moveToContact:moveToContact.replaceAll('\\s', '');
                accountToUpdate.add(new Account(Id = account.Id, Move_To_Contacts__c = moveToContact));
            }
            
        }
    }
    
    if(mapOfAccountIdAndString.size() > 0)
    {    
        for(Id accountId : mapOfAccountIdAndString.keySet()) 
        {
            String contactValue = '';
            for(String contact : mapOfAccountIdAndString.get(accountId))
            {
                contactValue = contactValue+contact+'\n';
            }
            accountToUpdate.add(new Account(Id = accountId, Contacts__c = contactValue));
        }
        
        if(accountToUpdate.size()>0) update accountToUpdate; 
    }
    
    if(setOfIds.size()>0)
    {
        
        List<Contact> contacts = [SELECT Id, Email, FirstName, LastName, Account.Move_To_Account__c, AccountId FROM Contact WHERE AccountId  IN :setOfIds];
        
        list<Contact> contactsTomoved = new List<Contact>();
        if(contacts.size()>0)
        {
            for(Contact contact : contacts)
            {
                for(String contString : mapOfAccountIdAndString.get(contact.Account.Move_To_Account__c))
                { 
                    
                    if(contact.FirstName == contString.substringAfter('FirstName=').substringBefore(',')
                       && contact.LastName == contString.substringAfter('LastName=').substringBefore(',')
                       && contact.Email == contString.substringAfter('Email=').substringBefore(','))
                    {
                        contact.AccountId = contact.Account.Move_To_Account__c;
                        contactsTomoved.add(contact);
                    }
                }
                
            }
        }
        
        if(contactsTomoved.size() > 0) update contactsTomoved;  
    }
 }
}
SwethaSwetha (Salesforce Developers) 
HI Sana,
Since this code provided requires an understanding of your implementation, it might not be possible to provide exact test class suggestions. 

However, the below articles give a good insight into how to begin writing test class and how coverage can be improved

https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test
https://salesforce.stackexchange.com/questions/10988/how-to-write-a-unit-test-test-class-for-trigger
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines 

Examples: https://salesforce.stackexchange.com/questions/248465/testclass-for-triggerhandler
https://salesforce.stackexchange.com/questions/236747/test-class-method-for-triggerhandler-negative-scenario

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you