You need to sign in to do that
Don't have an account?
SFDC@Error
Testing Code Coverage
Hi All
How can i improve my code coverage of my controller
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; } }
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.
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.
@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,