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
Devendra@SFDCDevendra@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

 

Best Answer chosen by Admin (Salesforce Developers) 
Devendra@SFDCDevendra@SFDC
Thanks a lot guys.
/* 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();

/* The below line will also cover catch block in checkDelete method */
        ApexPages.PageReference result1=ext.CheckDelete();

}
/* End Test Coverage */

 

The above code gives the 100% test coverage.

 

Cheers,

Devendra S

All Answers

lei.zhang@zuora.comlei.zhang@zuora.com

I think you need at least two test method to get 100% code coverage. one for no DMLException, another for handle DMLException.

 

BR.

Devendra@SFDCDevendra@SFDC

 

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.

 

 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;
     }

 

 

Thanks and Regards,

Devendra

lei.zhang@zuora.comlei.zhang@zuora.com

trigger a DMLException when execute "Database.delete(objHoliday); ", maybe you can try set objHoliday=null.

Devendra@SFDCDevendra@SFDC

 

Hi,

 

Can you specify in which line i need to make changes?

 

It would be helpful.

 

Regards,

Devendra S

Rahul SharmaRahul Sharma

try this:

 

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);
	DeleteHoliday ext=new DeleteHoliday(con);
	ApexPages.PageReference result=ext.CheckDelete();
	delete testH;
	ApexPages.PageReference result=ext.CheckDelete();
}

 

 

 

lei.zhang@zuora.comlei.zhang@zuora.com

=========================

 

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.

 

 

Devendra@SFDCDevendra@SFDC
Thanks a lot guys.
/* 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();

/* The below line will also cover catch block in checkDelete method */
        ApexPages.PageReference result1=ext.CheckDelete();

}
/* End Test Coverage */

 

The above code gives the 100% test coverage.

 

Cheers,

Devendra S

This was selected as the best answer
Devendra@SFDCDevendra@SFDC

 

Hello lei.zhang@zuora.com,

 

Holiday__c testH=null;
ApexPages.StandardController con=new ApexPages.StandardController(testH);

 It was not working for me.

 

Thanks a lot for reply.

 

Cheers,

Devendra S