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
Wanda BackWanda Back 

Apex Test Class How To Get 100% Code Coverage

Hello All,
I am an admin not a developer and I have written a simple before delete trigger and test class. The test is getting 80% coverage which is fine as I can deploy it, but for my own curiosity can anyone tell me how I can get 100% coverage please?

Thanks in advance

Trigger
trigger CheckGiftPledgeCount on Session__c (Before Delete) {
If(trigger.isBefore){
   If(Trigger.isDelete){
       for(Session__c a: trigger.old){
           if(a.TotalMembersRecruited__c>=1){
               a.adderror('You cannot delete a session if the count of total members recruited is greater than 0');
                 }
             }

        }
   }
}

Test Class
@isTest

private class CheckGiftPledgeCountTest{
     static testMethod void testTrigger(){
     Session__c ses = new Session__c() ;
     ses.Recruiter__c = '0035800000tXTlw';
     ses.Booking__c = 'a005800000K5yKl';

     insert ses;
     delete ses ;
     }
}

 
Best Answer chosen by Wanda Back
GauravGargGauravGarg
Please try this. 
@isTest
private class CheckGiftPledgeCountTest{

	@TestSetup
	private static void CreateTestDate(){
		Session__c ses = new Session__c() ;
		ses.Recruiter__c = '0035800000tXTlw';
		ses.Booking__c = 'a005800000K5yKl';

		insert ses;
	
		List<GiftPledge__c> gpList = new List<GiftPledge__c>();
		
		GiftPledge__c  gp1 = new GiftPledge__c (session__c = ses.id);
		GiftPledge__c  gp2 = new GiftPledge__c (session__c = ses.id);
		
		gplist.add(gp1);
		gpList.add(gp2);
		
		insert gplist;
		
	} 
     static testMethod void testTrigger(){
     Session__c ses = [SELECT Id FROM Session__c LIMIT 1];
	 
     delete ses ;
     }
}

All Answers

GauravGargGauravGarg
Hi Wanda,

Please try below code:
 
@isTest

private class CheckGiftPledgeCountTest{
     static testMethod void testTrigger(){
     Session__c ses = new Session__c() ;
     ses.Recruiter__c = '0035800000tXTlw';
     ses.Booking__c = 'a005800000K5yKl';

     insert ses;
     delete ses ;
     }
     
     static testMethod void testTrigger_error(){
     Session__c ses = new Session__c() ;
     ses.Recruiter__c = '0035800000tXTlw';
     ses.Booking__c = 'a005800000K5yKl';
     ses.TotalMembersRecruited__c = 2;
     insert ses;
     delete ses ;
     }

}


Thanks,

Gaurav
Skype: gaurav62990

 

Wanda BackWanda Back
Thanks for your reply Gaurav, so it would need a 2nd test that would fail as the session has TotalMembersRecruited =>1?
The problem is that the TotalMembersRecruited field is a Rollup Summary so I can't write to it, can I insert an existing session id that does have TotalMembersRecruited =>1 and run the test on that?
GauravGargGauravGarg

Hi Wanda,

TotalMembersRecruited  is rollup summary field for which object? Please insert two records for that object so that it will go within the if condition. 

Thanks,

Gaurav

Wanda BackWanda Back
Hi Gaurav,
The object is GiftPledge__c and 2 records are a0225000003ctK2 and a0225000003dHHS, can that be added to the test?
Thanks for your help, Wanda
GauravGargGauravGarg
Please try this. 
@isTest
private class CheckGiftPledgeCountTest{

	@TestSetup
	private static void CreateTestDate(){
		Session__c ses = new Session__c() ;
		ses.Recruiter__c = '0035800000tXTlw';
		ses.Booking__c = 'a005800000K5yKl';

		insert ses;
	
		List<GiftPledge__c> gpList = new List<GiftPledge__c>();
		
		GiftPledge__c  gp1 = new GiftPledge__c (session__c = ses.id);
		GiftPledge__c  gp2 = new GiftPledge__c (session__c = ses.id);
		
		gplist.add(gp1);
		gpList.add(gp2);
		
		insert gplist;
		
	} 
     static testMethod void testTrigger(){
     Session__c ses = [SELECT Id FROM Session__c LIMIT 1];
	 
     delete ses ;
     }
}
This was selected as the best answer
Wanda BackWanda Back
Thank you so much for your help, I just had to add a few required fields to the Gift Pledges and I now have 100%

Much appreciated!

Wanda
GauravGargGauravGarg
You welcome Wanda, it was my pleasure.