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
SFDC@ErrorSFDC@Error 

Testing Code Coverage

Hi All

How can i improve my code coverage of my controller
public class SampleRollupSummary {  
    
    public static void rollupContacts(list<Sales__c> lstOfconts){
        
        system.debug('==lstOfconts== : '+lstOfconts);
        
        set<id> accIds = new set<id>();
        
        list<Contact> updLstOfAccs = new list<Contact>();      
        
         double  totalAmount = 0;
        
        for(Sales__C con : lstOfconts){
            
            accIds.add(con.Customer_Name__c);
            
        }
        
        system.debug('==accIds==:'+accIds);
        
        list<Contact> lstAccs = [select id,Total_Sale__c,(select id,Sold_Price__c from Sales__r) from Contact where id in : accIds];
        
        
        
        for(Contact acc : lstAccs){
            
            
            
            for(Sales__c cpr : acc.Sales__r){
                
                totalAmount = totalAmount + cpr.Sold_Price__c ;
                
            }
            
            acc.Total_Sale__c= totalAmount;
            
            updLstOfAccs.add(acc);
            
        }
        
        if(updLstOfAccs.size() > 0){
            
            update updLstOfAccs;
            
        }
        
        
        
        
        
    }
    
    
    
}
 
@isTest
public with sharing class SampleRollupSummaryTest 
{ 
    static testmethod void validateStandardController()
    {
    Contact con  = new Contact(LastName = 'abc');
    Sales__c pro = new Sales__c();
    pro.sold_price__c=123;
   
     
    insert pro;
 
    insert con;
     
     
  
        
        
     
     }  
  
  
   
}

 
EswarForceEswarForce
Hi,
please try below test class i hope it will work fine.

@isTest public with sharing class SampleRollupSummaryTest {
      static testmethod void validateStandardController() {
         Contact con = new Contact(LastName = 'abc');
         INSERT con;
         Sales__c  sale = new Sales__c();
         sale.Customer_Name__c = con.id;
         sale.Sold_Price__c = 100;
        INSERT sale;
       
       }
}
It will satify your problem.please mark as solution it will help to others.
Regards,
Eswarforce.
Shamsi 110Shamsi 110
@isTest public with sharing class SampleRollupSummaryTest {
      static testmethod void validateStandardController() {
         Contact con = new Contact(LastName = 'Test');
         INSERT con;
         Sales__c  sale = new Sales__c();
         sale.Customer_Name__c = con.id;
         sale.Sold_Price__c = 100;
        INSERT sale;
     
       con .Total_Sale__c = 300;
       update con;
       }
}

If it works for you then mark this as a solution.
Regards,
Shamsi.
Ginny MahantGinny Mahant
Assuming the controller is not invoked by a trigger, you need to invoke it in your test class--

@isTest public with sharing class SampleRollupSummaryTest {
      static testmethod void validateStandardController() {
         Contact con = new Contact(LastName = 'abc');
         INSERT con;
         Sales__c  sale = new Sales__c();
         sale.Customer_Name__c = con.id;
         sale.Sold_Price__c = 100;
        INSERT sale;

        List<Sales__c> sc = new List<Sales__c>();
        sc.add(sale);
        SampleRollupSummary.rollupContacts(sc);
       }
}
If the suggestion resolved your issue, kindly mark it as best answer so as to help others identify this as a proper solution.

Regards,