You need to sign in to do that
Don't have an account?
Wanda 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 ;
}
}
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 ;
}
}
All Answers
Please try below code:
Thanks,
Gaurav
Skype: gaurav62990
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?
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
The object is GiftPledge__c and 2 records are a0225000003ctK2 and a0225000003dHHS, can that be added to the test?
Thanks for your help, Wanda
Much appreciated!
Wanda