0

i have below code in my apex class

global class Payment implements Comparable{
    public string id;
    public string YearQ;
    public string YearMo;
    public Date asOf;
    public decimal payment = 0;
    public decimal revenue = 0;
    public decimal attritionRate = 0;//DE420
    public decimal payOnRevenue = 0;//DE 420

    //only used by branch group sort (compareTo) to provide 0 values for missing payments
    public Payment() {
    }

    public Payment(PODA_Exhibit_Payment__c pmt) {
        this.id = pmt.id;
        this.YearQ = pmt.PODA_Contracted_Agency_Payment__r.YearQrt__c;
        this.YearMo = PODA_Util.getYearMoFromYearQ(this.YearQ);
        this.payment = pmt.Total_Payment__c;
        this.revenue = pmt.Total_Flown_Revenue__c;
        // DE 420 - Gain Calculator 9/9/14
        this.attritionRate = pmt.Attrition_Rate__c;
        this.payOnRevenue = pmt.Pay_On_Revenue__c;
    }

    public Payment(PODA_Branch_Payment_Summary__c pmt) {
        this.id = pmt.id;
        this.YearQ = pmt.YearQrt__c;
        this.YearMo = PODA_Util.getYearMoFromYearQ(this.YearQ);
        this.payment = pmt.Total_Payment__c;
        this.revenue = pmt.Total_Flown_Revenue__c;
    }

    public Payment(PODA_Branch_Exhibit_Payment__c pmt) {
        this.id = pmt.id;
        this.YearQ = pmt.YearQrt__c;
        this.YearMo = PODA_Util.getYearMoFromYearQ(this.YearQ);
        this.payment = pmt.Total_Payment__c;
        this.revenue = pmt.Total_Flown_Revenue__c;
    }

    public void addPayment(PODA_Branch_Payment_Summary__c pmt) {
      this.payment += pmt.Total_Payment__c;
      this.revenue += pmt.Total_Flown_Revenue__c;
    }

    public void addPayment(PODA_Branch_Exhibit_Payment__c pmt) {
        this.payment += pmt.Total_Payment__c;
        this.revenue += pmt.Total_Flown_Revenue__c;
    }

    public void addPayment(PODA_Exhibit_Payment__c pmt) {
        this.payment += pmt.Total_Payment__c;
        this.revenue += pmt.Total_Flown_Revenue__c;
        this.payOnRevenue += pmt.Pay_On_Revenue__c;
    }

    public Integer compareTo(Object compareTo) {
        Payment compareToHP = (Payment)compareTo;
        return this.YearQ.compareTo(compareToHP.YearQ);
    }

}

/// Payments is the inner class in the main apex class and i want to write test class for this.

I am facing issue while writing test for below method in the above code

 public Integer compareTo(Object compareTo) {
        Payment compareToHP = (Payment)compareTo;
        return this.YearQ.compareTo(compareToHP.YearQ);
    }

// i have created a class with name as Object1 in the test class and try to call the main class method as below

 PODA_Data.Payment objPODA = new PODA_Data.Payment();
 object1 obj = new object1();
  integer i = objPODA.compareTo(obj);

Facing error as below :

Error Message System.TypeException: Invalid conversion from runtime type PODA_Data_Test.object1 to PODA_Data.Payment

// could you please help me how to write test class for the above method.

1

I would write a test method (or multiple methods) for the compareTo of this form:

@IsTest
static void compareTo() {

    PODA_Data.Payment p1 = new PODA_Data.Payment(new PODA_Branch_Payment_Summary__c(YearQrt__c = '1'));
    PODA_Data.Payment p2 = new PODA_Data.Payment(new PODA_Branch_Payment_Summary__c(YearQrt__c = '2'));

    PODA_Data.Payment p3 = new PODA_Data.Payment(new PODA_Branch_Exhibit_Payment__c(YearQrt__c = '3'));
    PODA_Data.Payment p4 = new PODA_Data.Payment(new PODA_Branch_Exhibit_Payment__c(YearQrt__c = '4'));

    PODA_Data.Payment p5 = new PODA_Data.Payment();

    // TODO Also create PODA_Exhibit_Payment__c ones; bit more awkward

    System.assert(p2.compareTo(p1) > 0);
    System.assert(p1.compareTo(p2) < 0);
    System.assert(p1.compareTo(p1) == 0);

    System.assert(p4.compareTo(p3) > 0);
    System.assert(p3.compareTo(p4) < 0);
    System.assert(p3.compareTo(p3) == 0);

    System.assert(p3.compareTo(p1) > 0);
    System.assert(p1.compareTo(p3) < 0);

    // Method compareTo will NPE with this so test will highlight missing guard code
    System.assert(p5.compareTo(p1) < 0);

    ...
}

The point of tests is to make sure that code works so always include asserts for the required behaviour and cover a reasonable number of permutations.

0
1

I think you're not creating a Setup data for Payment properly. Instead of passing obj as Object type, pass payment as Payment type. Because by default every custom object inherits Object class. Rest of the code seems to be fine.

Up casting is handled by compiler itself but for down casting it will throw an error. Try below example:

static testMethod void  testCompareTo() {
    test.startTest();
    PODA_Data.Payment objPODA = new PODA_Data.Payment();
    Payment payment = new Payment();
    integer i = objPODA.compareTo(payment);
    test.stopTest();
}
1
  • Thanks for the response, i have tried the above but didn't worked Nov 13 '17 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.