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
Kunal Purohit 4Kunal Purohit 4 

How to implement test class for the given controller

Hello Folks, 

I am new to salesforce development. Attempting to write a test class for below code. Please suggest.
 

public class CountContactHandler {
    
    public static void ContactCheckBox(List<Contact> con) {
        
        Set<Id> setid = new Set<Id>();
        for(Contact c : con) {
            setid.add(c.AccountId);
        }
        
        List<Account> acc1 = [SELECT Id, Name From Account WHERE Id In :setid];
        
        for(Account a1 : acc1) {
            
        }
    }

}
Best Answer chosen by Kunal Purohit 4
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Kunal,

This apex class seems to be incomplete. But below test class covers 100% . If you share the trigger as well so it may cover both.
 
@istest
public class CountContactHandlerTest {
@isTest static void testconcount() {
    
    Account acc= new Account();
    acc.name= 'sample account';
    insert acc;
    
    Contact con= new Contact();
    con.lastname='sample cpontact';
    con.AccountId=acc.id;
    insert con;
    List<Contact> conlist= new List<Contact>();
    conlist.add(con);
    CountContactHandler.ContactCheckBox(conlist);
}
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
hi Kunal,

Is this handler for any trigger?

Thanks,
 
Kunal Purohit 4Kunal Purohit 4
Yeah, Sai Praveen
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Kunal,

This apex class seems to be incomplete. But below test class covers 100% . If you share the trigger as well so it may cover both.
 
@istest
public class CountContactHandlerTest {
@isTest static void testconcount() {
    
    Account acc= new Account();
    acc.name= 'sample account';
    insert acc;
    
    Contact con= new Contact();
    con.lastname='sample cpontact';
    con.AccountId=acc.id;
    insert con;
    List<Contact> conlist= new List<Contact>();
    conlist.add(con);
    CountContactHandler.ContactCheckBox(conlist);
}
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
Kunal Purohit 4Kunal Purohit 4
Thanks Sai Praveen. It worked.