You need to sign in to do that
Don't have an account?
Devendra@SFDC
Test Coverage Problem??
Hi,
I am using one object called Holiday__c. It has two fields Name and Date.
The following code represents the code for deleting Holiday record.
public class DeleteHoliday { /* Global Variables */ private final Holiday__c objHoliday; /* End of Global Variables */ /* Standard Controller Constructor */ public DeleteHoliday(ApexPages.StandardController con) { this.objHoliday=(Holiday__c)con.getrecord(); } /* End of Standard Controller Constructor */ /* Method called on to Delete Holiday */ public PageReference checkDelete() { try { Database.delete(objHoliday); } catch(System.DMLException e) { return null; } PageReference p = new Pagereference('/apex/AdminListView?intFocus=4’); p.setRedirect(true); return p; } }
The following code represents the Test Coverage code:
/* Test Coverage */ static testMethod void deleteHolidayPositiveTest() { Holiday__c testH=new Holiday__c(Name='Test HL1',date__c=System.Today()); insert testH; ApexPages.StandardController con=new ApexPages.StandardController(testH); /* Instantiate the extention instance with the standardcontroller */ DeleteHoliday ext=new DeleteHoliday(con); /* Invoke the delete method on the extension */ ApexPages.PageReference result=ext.CheckDelete(); } /* End Test Coverage */
By using the above test code i am getting 81% test coverage. I am looking for 100% test coverage. What changes are required to gain 100% test coverage?
Thanks and Regards,
Devendra S
The above code gives the 100% test coverage.
Cheers,
Devendra S
All Answers
I think you need at least two test method to get 100% code coverage. one for no DMLException, another for handle DMLException.
BR.
Hi,
Here i am covering for DML operation. How can i cover for No DML operation??
I am not able to cover the " return null; " line.
Thanks and Regards,
Devendra
trigger a DMLException when execute "Database.delete(objHoliday); ", maybe you can try set objHoliday=null.
Hi,
Can you specify in which line i need to make changes?
It would be helpful.
Regards,
Devendra S
try this:
=========================
Holiday__c testH=new Holiday__c(Name='Test HL1',date__c=System.Today());
insert testH;
ApexPages.StandardController con=new ApexPages.StandardController(testH);
=========================
change to
Holiday__c testH=null;
ApexPages.StandardController con=new ApexPages.StandardController(testH);
Maybe it works, Not sure.
The above code gives the 100% test coverage.
Cheers,
Devendra S
Hello lei.zhang@zuora.com,
It was not working for me.
Thanks a lot for reply.
Cheers,
Devendra S