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
rameshramesh 

how to write class in test class my code is........

public class acc_contact_num_rollup 
{
    public static Void InsertMethod(list<Contact> lstCon ){
        List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
         for(Contact con : lstCon){ 
            if(con.AccountId != null){
                   setAccIds.add(con.AccountId);      
            }   
        }
        for(Account acc :[Select id,Number_of_Contact__c ,(Select id,name from contacts) from Account where Id in : setAccIds]){
     
        acc.Number_of_Contact__c = acc.contacts.size();
       
        acclist.add(acc);
    }
    if(!acclist.isempty()){
        update accList; 
    }
    }
    public static Void UpdateMethod(list<Contact> lstCon,map<Id,Contact>oldmap ){
        List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
         for(Contact con : lstCon){ 
            if(con.AccountId != null){
                   setAccIds.add(con.AccountId); 
                setAccIds.add(oldMap.get(con.Id).AccountId);
            }   
        }
        for(Account acc :[Select id,Number_of_Contact__c ,(Select id,name from contacts) from Account where Id in : setAccIds]){
     
        acc.Number_of_Contact__c = acc.contacts.size();
       
        acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList; 
    }
    }
    public static Void deleteMethod(list<Contact> lstCon){
        List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
         for(Contact con : lstCon){ 
            if(con.AccountId != null){
                   setAccIds.add(con.AccountId);      
            }   
        }
        for(Account acc :[Select id,Number_of_Contact__c ,(Select id,name from contacts) from Account where Id in : setAccIds]){
     
        acc.Number_of_Contact__c = acc.contacts.size();    
        acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList; 
    }
    }

}
Best Answer chosen by ramesh
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sai ,

Can you try the below test class which gives you 100% coverage  for the class and even for the trigger using this class as well. We are creating Account and Contact checking the results based on our code .
 
@isTest 
public class acc_contact_num_rollupTest {
static testMethod void testMethod1() 
 {
 Account testAccount = new Account();
 testAccount.Name='Test Account' ;
     insert testAccount;
     Account testAccount1 = new Account();
 testAccount1.Name='Test Account' ;
     insert testAccount1;

     contact con= new Contact();
     con.lastname='sample';
     con.AccountId= testAccount.id;
     insert con;
     List<Account> acclisr= [select id,Number_of_Contact__c from Account where Id = :testAccount.id];
     
system.assertEquals(1 ,acclisr[0].Number_of_Contact__c );
     
     Con.AccountId= testAccount1.id;
     update con;
     List<Account> acclisr1= [select id,Number_of_Contact__c from Account where Id = :testAccount.id];
     
system.assertEquals(0 ,acclisr1[0].Number_of_Contact__c );
     delete con;
     List<Account> acclisr2= [select id,Number_of_Contact__c from Account where Id = :testAccount.id];
     
system.assertEquals(0 ,acclisr2[0].Number_of_Contact__c );

 
 }
}
Let me know if you face any issue in the code
If this solution helps, Please mark it as best answer.

Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sai ,

Can you try the below test class which gives you 100% coverage  for the class and even for the trigger using this class as well. We are creating Account and Contact checking the results based on our code .
 
@isTest 
public class acc_contact_num_rollupTest {
static testMethod void testMethod1() 
 {
 Account testAccount = new Account();
 testAccount.Name='Test Account' ;
     insert testAccount;
     Account testAccount1 = new Account();
 testAccount1.Name='Test Account' ;
     insert testAccount1;

     contact con= new Contact();
     con.lastname='sample';
     con.AccountId= testAccount.id;
     insert con;
     List<Account> acclisr= [select id,Number_of_Contact__c from Account where Id = :testAccount.id];
     
system.assertEquals(1 ,acclisr[0].Number_of_Contact__c );
     
     Con.AccountId= testAccount1.id;
     update con;
     List<Account> acclisr1= [select id,Number_of_Contact__c from Account where Id = :testAccount.id];
     
system.assertEquals(0 ,acclisr1[0].Number_of_Contact__c );
     delete con;
     List<Account> acclisr2= [select id,Number_of_Contact__c from Account where Id = :testAccount.id];
     
system.assertEquals(0 ,acclisr2[0].Number_of_Contact__c );

 
 }
}
Let me know if you face any issue in the code
If this solution helps, Please mark it as best answer.

Thanks,
 
This was selected as the best answer
CharuDuttCharuDutt
Hii Sai Kiran 
Try Below Code Coverage 100%
@isTest
public class acc_contact_num_rollupTest {
	@isTest
    public static void UnitTest(){
        list<Contact> lstCon = new list<Contact>();
        list<Contact> lstdeleteCon = new list<Contact>();
        Account Acc = new Account();
        Acc.Name = 'Test Account ';
        insert Acc;

        contact con = new Contact();
        con.lastName = 'Test Contact';
        Con.AccountId = Acc.id;
        Insert Con;
        lstCon.add(Con);
        delete con;
        lstdeleteCon.add(Con);

        
        acc_contact_num_rollup.InsertMethod(lstCon);
       acc_contact_num_rollup.deleteMethod(lstdeleteCon);
    }
    @isTest
    public static void UnitTest2(){
        list<Contact> lstCon = new list<Contact>();
        Map<Id,Contact> MapCon = new Map<Id,Contact>();
        Account Acc = new Account();
        Acc.Name = 'Test Account ';
        insert Acc;
        Account Acc2 = new Account();
        Acc2.Name = 'Test Account 2';
        insert Acc2;

        contact con = new Contact();
        con.lastName = 'Test Contact';
        Con.AccountId = Acc.id;
        Insert Con;
        MapCon.put(Con.Id, Con);
        Con.AccountId = Acc2.id;
        update Con;
        lstCon.add(Con);
       
acc_contact_num_rollup.UpdateMethod(lstCon, MapCon);
    }
    
    
    
}
Please Mark It As Best Answer If It Helps
Thank You!
rameshramesh
thanks praveen and charu both working fine thanks so much your helping