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
Surya PSurya P 

Test class for Insert and Update of multiple contacts

I have wrote a test class for an Apex class,but the code coverage is 42%.

Kindly help me where I'm wrong in the code.

Apex Class:
public class InsertContactsToAccount {
    
    
   @AuraEnabled
    public static void saveContacts(List<Contact> listContact, Id recordId)
    {
        list<contact> conlist = new list<contact>();
        Insert listContact;
        for(Contact a:listContact){
            a.AccountId = recordId;
            conlist.add(a);
        }
        update conlist;
    } 
}

Test Class:
 
@isTest
public class InsertContactsToAccountTest {
    static testMethod void saveContactsTest(){
        
        Account a = new Account();
        a.Name ='Test';
        insert a;
        
        Contact c = new Contact();
        c.LastName = 'Test';
        insert c;
        
        c = [select id from Contact where id =:c.id];
        c.AccountId = a.Id;
        update c;
        
        List<Contact> con = [select id from Contact where id =:c.id];
        InsertContactsToAccount.saveContacts(con,a.id);
        
    }

}

Thanks in Advance 

Regards
Surya
Best Answer chosen by Surya P
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi surya,
When creating unit test you just have to provide all the data or records needed for you saveContacts method and call that method.
Here your save contact method has List<Contact> (list of contacts) which are not inserted before and second parameter id (which is account id).
So you have to create List<Contact> and create an account and pass its id.

Try this code.
@isTest
public class InsertContactsToAccountTest {
    
    static testMethod void saveContactsTest(){
        list<contact> cons = new list<contact>();
        Account a = new Account();
        a.Name ='unit Test';
        insert a;
        
        Contact c = new Contact();
        c.LastName = 'unit Test contact';
        cons.add(c);

        Contact c1 = new Contact();
        c1.LastName = 'Test1';
        cons.add(c1);
        InsertContactsToAccount.saveContacts(cons,a.id);
        
    }

}

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards

All Answers

Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi surya,
When creating unit test you just have to provide all the data or records needed for you saveContacts method and call that method.
Here your save contact method has List<Contact> (list of contacts) which are not inserted before and second parameter id (which is account id).
So you have to create List<Contact> and create an account and pass its id.

Try this code.
@isTest
public class InsertContactsToAccountTest {
    
    static testMethod void saveContactsTest(){
        list<contact> cons = new list<contact>();
        Account a = new Account();
        a.Name ='unit Test';
        insert a;
        
        Contact c = new Contact();
        c.LastName = 'unit Test contact';
        cons.add(c);

        Contact c1 = new Contact();
        c1.LastName = 'Test1';
        cons.add(c1);
        InsertContactsToAccount.saveContacts(cons,a.id);
        
    }

}

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards
This was selected as the best answer
Surya PSurya P
Hi Devi,

Thank you so much for the clear explanation.

Regards.
Surya