1

I've recently been writing a few basic controllers for some visual force pages, and of course now need to test them. Problem is I don't really know how. Normally you know you'd instantiate the class, pass data to the methods, and assert against the results right? With this controller I don't know what to test against.

It would be great if someone could help me write a test class for this

public with sharing class PropertyExtensionClass {
private ApexPages.StandardController ctrl; 

public PropertyExtensionClass(ApexPages.StandardController ctrlParam){
    ctrl = ctrlParam;
}
public void PropCalc(){
  Property__c  p = (Property__c ) ctrl.getRecord();
  Id Propid = (Id) ctrl.getRecord().get('id');
  Property__c v_p =[SELECT Gross_Profit__c,Unlevered_ROE__c FROM Property__c where Id=:Propid];    
  system.debug('Property values'+v_p);  
  decimal result;
    if(v_p.Unlevered_ROE__c >= 0 && v_p.Unlevered_ROE__c<2 )
    {
        system.debug('Success');
        result = 1000;
    }

    else if(v_p.Unlevered_ROE__c >= 2 && v_p.Unlevered_ROE__c<4 )
    {
        system.debug('Success');
        result = (v_p.Gross_Profit__c * 5)/100;
    }

    else if(v_p.Unlevered_ROE__c >= 4 && v_p.Unlevered_ROE__c<5 )
    {
        system.debug('Success');
        result = (v_p.Gross_Profit__c * 7)/100;
    }

    else if(v_p.Unlevered_ROE__c >= 5 && v_p.Unlevered_ROE__c<6 )
    {
        system.debug('Success');
        result = (v_p.Gross_Profit__c * 9)/100;
    }

    else if(v_p.Unlevered_ROE__c >= 6 && v_p.Unlevered_ROE__c<7 )
    {
        system.debug('Success');
        result = (v_p.Gross_Profit__c * 13)/100;
    }

    else if(v_p.Unlevered_ROE__c >= 7 && v_p.Unlevered_ROE__c<8 )
    {
        system.debug('Success');
        result = (v_p.Gross_Profit__c * 17)/100;
    }

    else if(v_p.Unlevered_ROE__c >= 8 && v_p.Unlevered_ROE__c<9 )
    {
        system.debug('Success');
        result = (v_p.Gross_Profit__c * 19)/100;
    }

    else if(v_p.Unlevered_ROE__c > 10)
    {
        system.debug('Success');
        result = (v_p.Gross_Profit__c * 22)/100;
    }

    Property__c p1 = new Property__c();
    p1.Id = Propid;
    p1.Actual_Payout__c = result;
   update p1;
} 
}
  • Why not just use a Flow and skip the hassle of deploying code/unit tests/etc? – sfdcfox Jul 13 '16 at 10:23
  • i am using visualforce page – user33233 Jul 13 '16 at 10:28
2

Your code has a bug. For values of Unlevered ROE between 9 inclusive and 10 exclusive (9 to 9.999999...), your code will render "actual payout" as null; the same is true if the value is less than 0. You can avoid this problem by restructuring your logic:

decimal result;
if(v_p.Unlevered_ROE__c == null || v_p.Gross_Profit__c == null || v_p.Unlevered_ROE__c < 0) {
    result = 0; // Change if necessary
} else if(v_p.Unlevered_ROE__c < 2) {
    result = 1000;
} else if(v_p.Unlevered_ROE__c < 4) {
    result = (v_p.Gross_Profit__c * 5)/100;
} else if(v_p.Unlevered_ROE__c < 5) {
    result = (v_p.Gross_Profit__c * 7)/100;
} else if(v_p.Unlevered_ROE__c < 6) {
    result = (v_p.Gross_Profit__c * 9)/100;
} else if(v_p.Unlevered_ROE__c < 7) {
    result = (v_p.Gross_Profit__c * 13)/100;
} else if(v_p.Unlevered_ROE__c < 8) {
    result = (v_p.Gross_Profit__c * 17)/100;
} else if(v_p.Unlevered_ROE__c < 9) {
    result = (v_p.Gross_Profit__c * 19)/100;
} else { // Equal to or more than 9
    result = (v_p.Gross_Profit__c * 22)/100;
}

Also, you could have saved yourself some trouble by just using the record you've already been given:

public with sharing class PropertyExtensionClass {
    ApexPages.StandardController ctrl;

    public PropertyExtensionClass(ApexPages.StandardController ctrlParam){
        ctrl = ctrlParam;
    }
    public void PropCalc(){
        Property__c v_p = (Property__c)ctrl.getRecord();
        // Do calculations here from above, then save.

        ctrl.save();
    }

At this point, your unit tests should test every possible condition you care about.

@isTest class testProperyExtensionClass {
    static ApexPages.StandardController createProperty(Decimal unleveredRoe) {
        Property__c p = new Property__c(Gross_Profit__c=100, Unlevered_ROE__c=unleveredRoe);
        insert p;
        return new ApexPages.StandardController(p);
     }
     @isTest static void testNullRoe() {
         PropertyExtensionsClass p = new PropertyExtensionsClass(createProperty(null));
         p.PropCalc();
         System.assertEquals(0, [SELECT Actual_Payout__c FROM Property__c].Actual_Payout___c);
      }
}

Then, do the rest of your methods, which is basically copy-paste:

     @isTest static void testRoeLessThan2() {
         PropertyExtensionsClass p = new PropertyExtensionsClass(createProperty(1.5));
         p.PropCalc();
         System.assertEquals(1000, [SELECT Actual_Payout__c FROM Property__c].Actual_Payout___c);
      }
      // Do other conditions, one per test
| improve this answer | |

Your Answer

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