You need to sign in to do that
Don't have an account?

Assistance with a test class to increase code coverage
Hi, I'm struggling with test class to increase test coverage, currently got only 68%, so all suggestions welcome
my trigger:
my class:
thank you in advance
my trigger:
/* Provide summary of Number of Contacts on Account record https://success.salesforce.com/answers?id=90630000000h3mNAAQ */ trigger ContactSumTrigger on Contact (after delete, after insert, after undelete, after update) { Contact[] cons; if (Trigger.isDelete) cons = Trigger.old; else cons = Trigger.new; // get list of accounts Set<ID> acctIds = new Set<ID>(); for (Contact con : cons) { acctIds.add(con.AccountId); } Map<ID, Contact> contactsForAccounts = new Map<ID, Contact>([select Id ,AccountId from Contact where AccountId in :acctIds]); Map<ID, Account> acctsToUpdate = new Map<ID, Account>([select Id ,Number_of_Contacts__c from Account where Id in :acctIds]); for (Account acct : acctsToUpdate.values()) { Set<ID> conIds = new Set<ID>(); for (Contact con : contactsForAccounts.values()) { if (con.AccountId == acct.Id) conIds.add(con.Id); } if (acct.Number_of_Contacts__c != conIds.size()) acct.Number_of_Contacts__c = conIds.size(); } update acctsToUpdate.values(); }
my class:
@isTest public class ContactSumTriggerTest { static testmethod void TestContactTrgr() { Test.startTest(); // Create account Account ac = new Account(); ac.Name = 'Test Account'; ac.Mailing_Country__c = 'United Kingdom'; Insert ac; system.debug('Completed Account Creation'); // Update account Database.SaveResult sr2 = Database.update(ac, true); System.assert(sr2.isSuccess()); system.debug('Account updated'); delete ac; undelete ac; // Create 1st contact Contact ct1 = new Contact(); ct1.FirstName = 'FirstName 1'; ct1.LastName = 'LastName 1'; ct1.Email = 'test1@test.com'; ct1.Mailing_Country__c = 'United Kingdom'; ct1.Account = ac; Insert ct1; system.debug('Completed Contact Creation'); // Update 1st contact Database.SaveResult sr2 = Database.update(ct1, true); System.assert(sr2.isSuccess()); system.debug('Contact updated'); delete ct1; undelete ct1; // Create 2nd contact Contact ct2 = new Contact(); ct2.FirstName = 'FirstName 2'; ct2.LastName = 'LastName 2'; ct2.Email = 'test2@test.com'; ct2.Mailing_Country__c = 'United Kingdom'; ct2.Account = ac; Insert ct2; system.debug('Completed Contact Creation'); // Update 2nd contact Database.SaveResult sr2 = Database.update(ct2, true); System.assert(sr2.isSuccess()); system.debug('Contact updated'); delete ct2; undelete ct2; Test.stopTest(); } }
thank you in advance
You can simply use this code snippet. I have put the above code to 'Run Test' and it is already showing the Code Coverage as 100%.
Mark the answer as Solution so that developers browsing around can look directly to the answer that provides a solution.
Cheers Mate ! :)
All Answers
You can simply use this code snippet. I have put the above code to 'Run Test' and it is already showing the Code Coverage as 100%.
Mark the answer as Solution so that developers browsing around can look directly to the answer that provides a solution.
Cheers Mate ! :)
I wasn't aware that in dev console you can check which part of the clas is not covered, lessonfor me that notepad ++ is not always the best tool