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
arun kumar.ax887arun kumar.ax887 

How to make the TestCase successful.

Hi All,

 

    I have a requirement, where in I need to delete all the Invoice Payment Line Items which has a specific record type.  For which I have written a class.  The class is working properly and we are getting the desired output, but when I am writing the test case the test case is failing and the code coverage is 54%.  

 

 

  Therefore I request you people to kindly suggest a solution such that the test case is successful and code coverage is above 75% as I need to migrate the code.

 

 

I am sending the code of  both Apex class and as well as TestCase.  Kindly look into the code and provide an appropriate solution.

 

Apex Class

 

 

public with sharing class Dev_ControlDeleteon_INV {
 String recordId;
 
    public Dev_ControlDeleteon_INV (ApexPages.StandardController controller) {recordId = controller.getId();}
    
public PageReference redirect() {
Invoice_Payment_Line_Item__c jkl;
 RecordType[] rt = [select Id,Name from RecordType];
Invoice_Payment_Line_Item__c dr=[select id,name,recordtypeId,Invoice_Number__c from Invoice_Payment_Line_Item__c where id =:recordId];
for(RecordType yt :rt){
if(dr.recordtypeId !='01290000000Kex7'){
delete dr;
  
  
  return new PageReference('/'+dr.Invoice_Number__c +'/d?retURL=%2F'+dr.Invoice_Number__c);
}
}
return null;
}
}
TestCase
@istest
private class Dev_ControlDeleteon_INVTestCase {
     static testMethod void testMyController() 
     {
   
      ApexPages.StandardController con = new ApexPages.StandardController(new Invoice_Payment_Line_Item__c());
      
      Dev_ControlDeleteon_INV  dc = new Dev_ControlDeleteon_INV(con);
      
      PageReference pr1 = dc.redirect();
      
      
       }
}

 
Regards

Arun

 

Slaviša MaslićSlaviša Maslić

Hi Arun,

 

in the testmethod, you should at least insert one instance of the Invoice_Payment_Line_Item__c object, i.e.

 

static testMethod void testMyController() 

     {

     Invoice_Payment_Line_Item__c  myInvoice = new Invoice_Payment_Line_Item__c(Name='testABC', RecordTypeId='01290000000Kex8', do initialization here);

     insert myInvoice;

 

// and pass the instanct to your standard controller
 
      ApexPages.StandardController con = new ApexPages.StandardController(myInvoice);
      
      Dev_ControlDeleteon_INV  dc = new Dev_ControlDeleteon_INV(con);
      
      PageReference pr1 = dc.redirect();
      
      
       }

 

You should of course follow the best practices for writing test methods and writing code that doesn't hit SFDC-limits, which is explained in several resources available on the developer.force.com page. E.g. you should not call DML-method inside a for-loop.

 

Kind regards,

smash