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
sekharasekhara 

Improvement of Test Case

Hi to all,

Can any one Help to increase the code coverage @ present i am getting 75% .

Code in not covered in Red Part .

So please help regarding this part.

 

Apex Class:

 

 

public class Dev_Package_RNController
{
   public Dev_Package_RNController(ApexPages.StandardController controller) {
   
 }
Public Final RN__c rn;
Public Final RN_Payment_Line_Item__c rnpli;
public Dev_Package_RNController()
 {
   rn = [Select id,Dev_Date__c ,Dev_Patient_Name__c ,Dev_Printable_Address__c ,Dev_Patient_Telephone__c,Dev_Package_Branch__c ,Dev_Package_Invoice__c ,
Dev_Patient_Identification__c,Dev_Printable_Address_2__c ,Dev_Package_Item__c,Dev_Utilised_Treatment__c ,Dev_Package_Amount_Paid__c ,Dev_Refund_Admin_Fee__c ,Dev_Nett_Amount_After_Admin_Fee__c ,Dev_Qty_used__c,Dev_Average_Treatment_Price__c ,Dev_Amount_Refundable__c, Dev_Refund_Branch_Logo_URL__c
      from RN__c where id = :ApexPages.currentPage().getParameters().get('id')];
 
   try{
rnpli = [Select id ,name , RecordTypeId ,Dev_Payment_Description__c ,Refund_Note_Number__c,
   Dev_Payment_Date__c,Dev_Amount_Refunded__c  from RN_Payment_Line_Item__c 
   where RecordTypeId= '01290000000KV51' and Refund_Note_Number__c =:rn.id ];
} catch(System.QueryException e){
 
}
    
      }
   public String getname() {
return 'Dev_Package_RNController';
}
public RN__c getRN() {
return rn;
}
public RN_Payment_Line_Item__c getRNpli() {
return rnpli;
}
}

 

 

 

Test Case:

 

 

@isTest

 

private class Dev_Package_RNController_Test{

 

static testmethod void testPackageController()

   {

 

 ApexPages.StandardController con = new ApexPages.StandardController(new rn__c());

 

     Dev_Package_RNController Control  = new Dev_Package_RnController(con); 

 

     control.getname();

     control.getRN();

      control.getRNpli();

 

   Dev_Package_RNController Control1 = new Dev_Package_RnController(con); 

          }

}

 

Pratibh PrakashPratibh Prakash

Hello,

 

Just add this line:

 

 Dev_Package_RNController obj = new  Dev_Package_RNController();

 

Regards,

PP

 

Jake GmerekJake Gmerek

Although adding the line:

 

 Dev_Package_RNController obj = new  Dev_Package_RNController();

 

will call the appropiate section of code, there is more you need to add to your code.  Essentially what you have to remember first of all is that when writing test code you should never assume that there is data in your database.  So you should create a RN__c and RN_Payment_Line_Item__c and insert them into the database.  So you should add the following:

 

RN__c RN1 = new RN__c(field1Name=field1Value, etc);

RN_Payment_Line_Item__c RNPLI1 = new RM_Payment_Line_Item__c(some list);

insert RN1;

insert RNPLI1;

 

In addition your code calls ApexPages.currentPage().getParameters().get('id') so you need to set the current page of reference.  For this you could use the following:

 

PageReference pageRef = new PageReference('/apex/myPage?id='+ RN1.id);
Test.setCurrentPageReference(pageRef);

 

Finally, it is good practice to assert that your code performed as desired with something like:

 

system.assertEquals (value1, value2);

 

So putting it all together your code should look something like:

 

 

@isTest
 
private class Dev_Package_RNController_Test{
 
static testmethod void testPackageController()
   {
ApexPages.StandardController con = new ApexPages.StandardController(new rn__c());

RN__c RN1 = new RN__c(field1Name=field1Value, etc);
RN_Payment_Line_Item__c RNPLI1 = new RM_Payment_Line_Item__c(some list);
insert RN1;
insert RNPLI1;

PageReference pageRef = new PageReference('/apex/myPage?id='+ RN1.id);
Test.setCurrentPageReference(pageRef);
 
     test.startTest();  //start actual testing
     Dev_Package_RNController Control  = new Dev_Package_RnController(con); 
     Dev_Package_RNController Control1 = new Dev_Package_RnController(); 

     string returnedName = control.getname();
     RN__c returnedRN = control.getRN();
     RN_Payment_Line_Item__C returnedRNPLI = control.getRNpli();

     test.stopTest();  //testing complete

//below this you should re-query both of the objects that you inserted 
//above and then assert that they equal the ones from above 
//for example

system.assertEquals(returnedName, 'Dev_Package_RNController');

//and so on 

   
          }
}

 Obviously there are some values to change and I will leave it to you to finish the assert statements, but I hope this helps.

 

 

 

Shashikant SharmaShashikant Sharma

You are creating instance of other constructor

 

Your test method should be like this

 

 

@isTest
 
private class Dev_Package_RNController_Test{
 
static testmethod void testPackageController()
   {
 
 ApexPages.StandardController con = new ApexPages.StandardController(new rn__c());
 
     Dev_Package_RNController Control  = new Dev_Package_RnController(con); 
 
     Control  = new Dev_Package_RnController(); 
     control.getname();
     control.getRN();
      control.getRNpli();
 
   Dev_Package_RNController Control1 = new Dev_Package_RnController(con); 
          }
}