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
ArunKumar_RemmaArunKumar_Remma 

How to make the test case successful

Hi,

 

    I have a requirement, where in we need to pay the commissions on the payment for the plot in a hierarchial order.  We have written the class which is working perfectly, but when I tried to write the test case for the class, the test case is getting failed.  Can anyone suggest a solution so that the test case get successful with a code coverage above 75% so as migrate the application to the production environment.

 

  I am writing both Apex class and Test case, do look into the code and suggest a solution for the above requirement.

 

 

Apex Class

 

public class paycommissioncontroller {
    
    private ApexPages.StandardController controller {get; set;}
    
     
    public distributor__c d {get;set;}
    public payment__c p;
    static double prevcommpercent;
    static double currentcommamt;
   
    public paycommissioncontroller(ApexPages.StandardController controller)
    {
    this.controller = controller;
    p = (payment__c)controller.getRecord();
    }
 
        
    // method called from the VF's action attribute to clone the Payments
    
    public PageReference paycom() {
    prevcommpercent=0;
    currentcommamt=0;
       
    p=[select id,name,pay__c,plot__c,CUSTOMER__c,No_of_commision__c ,CUSTOMER__r.Customer_Type__c ,Payment_Amount__c,Distributor__c from payment__c where id =:p.id];    
 
 
    // Get all The Commissions already Exist if Any and Delete Them,Every time when "pay commission" is clicked if you want to generate new commissions use following two line and delete next If Condition
    
  
  // Create New Commissions
 
    List<commission__c> createcommission = new List <commission__c> {};
    if (p.CUSTOMER__r.Customer_Type__c == 'agrigold'&& p.Distributor__c!='a0JO0000000e5vq'&& p.No_of_commision__c==0) {
    d=[select id,name,reports_to__c,Eligible_Commission__c from distributor__c where id=:p.distributor__c];
    while(d!=null)
    {
    distributor__c[] ds;
    currentcommamt=(d.Eligible_Commission__c-prevcommpercent);

    prevcommpercent=d.Eligible_Commission__c;

    createcommission.add(new commission__c (DISTRIBUTOR__c=d.id,Payment_Amount__c=p.Payment_Amount__c,
        CUSTOMER__c=p.CUSTOMER__c,Plot_No__c=p.plot__c,PAYMENT_ID__c=p.id,Commission__c=(currentcommamt)));


    ds=[select id,name,reports_to__c,Eligible_Commission__c from distributor__c where id=:d.reports_to__c];
if(ds.size()>0)
{
d=ds[0];
}
else
{
d=null;
}
}
}

    else if(p.CUSTOMER__r.Customer_Type__c == 'CRM'&&p.No_of_commision__c==0){
    d=[select id,name,reports_to__c,Eligible_Commission__c from distributor__c where id=:p.distributor__c];

    distributor__c[] ds;
    prevcommpercent=0;
    currentcommamt=0;
    currentcommamt=18;
    prevcommpercent=18;

createcommission.add(new commission__c (DISTRIBUTOR__c=d.id,Payment_Amount__c=p.Payment_Amount__c,
CUSTOMER__c=p.CUSTOMER__c,Plot_No__c=p.plot__c,PAYMENT_ID__c=p.id,Commission__c=(currentcommamt)));

createcommission.add(new commission__c (DISTRIBUTOR__c='a0JO0000000e5vq',Payment_Amount__c=p.Payment_Amount__c,
CUSTOMER__c=p.CUSTOMER__c,Plot_No__c=p.plot__c,PAYMENT_ID__c=p.id,Commission__c=(5)));

}
insert createcommission;



               return (new ApexPages.StandardController(p)).view();          
           
   
         }
         
         
   }

 

 

TestCase

@isTest

private class PayCommissionControllerTestCase{

static testmethod void testPayCommissionController()
   {
          
      ApexPages.StandardController con = new ApexPages.StandardController(new Payment__c());
  
      PayCommissionController pcc = new PayCommissionController(con);
      
      PageReference pr1 = pcc.paycom();
         
       
   }

}

 

 

     For the above Testcase I am getting a code coverage of 35% and also the testcase is failing, therefore I request you people to suggestion some solution so that the test case is successful and code coverage is above 75 %.

 

 

 

 

Thanks and Regards

 

Arun

arizonaarizona

Let me help you fix it yourself.

 

On the right side of eclipse there is a debug log.  You can do a [ctrl] [a] to select the text and a [ctrl] [c] to copy the text.  Paste the contents into a text editor of your choice.

 

You should be able to see where it failed and that will give you a clue as to what went wrong.

 

I hope this helps.

 

narizona

arizonaarizona

I think the problem is that you are not setting an id so the constructor is failing.

 

PageReference pageRef = Page.(Name of your page)

 

// You can query for a valid Id and use it below.

pageRef.getParameters().put('Id', someValidId);

 

// This sets the page up for the test.
Test.setCurrentPage(pageRef);

 

      ApexPages.StandardController con = new ApexPages.StandardController(new Payment__c());
 
      PayCommissionController pcc = new PayCommissionController(con);
      
      PageReference pr1 = pcc.paycom();