1

I need to get code coverage on UpgradeRate, PlusCount2, PremiumCount2 and PrestigeCount2, but what I have is not working. How do I get code coverage on these? In my custom controller, I have the following wrapper class:

 public class employee{
        public Decimal PlusCount{get;set;}
        public Decimal PlusCount2 {get{if(PlusCount == 0){return 0;}else return PlusCount.setScale(0);}}
        public Decimal PremiumCount{get;set;}
        public Decimal PremiumCount2 {get{if(PremiumCount == 0){return 0;}else return PremiumCount.setScale(0);}}
        public Decimal PrestigeCount{get;set;}
        public Decimal PrestigeCount2 {get{if(PrestigeCount == 0){return 0;}else return PrestigeCount.setScale(0);}}
        public Decimal UpgradeRate{
            get{ 
                if(PlusCount == 0 &&(PrestigeCount != 0 || PremiumCount != 0)){
                    return 100;
                } else if(PlusCount == 0 && PrestigeCount == 0 && PremiumCount == 0){
                    return 0; 
                } else return ((PremiumCount + PrestigeCount) / (PremiumCount + PrestigeCount + PlusCount)).setScale(3) * 100;
            }
        }

        public employee(AggregateResult i){

            PlusCount = (Decimal)i.get('plus_count');
            PremiumCount = (Decimal)i.get('premium_count');
            PrestigeCount = (Decimal)i.get('prestige_count');

        }
    }

I am able to get code coverage for PlusCount, PremiumCount, and PrestigeCount, with the following test class:

List<FranchiseMetrics.employee> employees = new List<FranchiseMetrics.employee>();
AggregateResult[] employeeInspections = new AggregateResult[]{};

 employeeInspections = [SELECT Count(id) Total,Sum(Number_of_Home_Inspections__c) HomeInspections, AVG(Invoice_Sub_Total__c) AJS, Sum(Invoice_Sub_Total__c) TotalDollars,Sum(Added_Service_Cost__c) ServiceCost,
                    Sum(Home_Inspection_dollars__c) HomeDollars, Inspector__r.FirstName Name, 
                    Sum(Plus_Sales__c) plus_dollars, Sum(Premium_Sales__c) premium_dollars, Sum(Prestige_Sales__c) prestige_dollars,
                    Sum(of_Plus_Sales__c) plus_count, Sum(of_Premium_Sales__c) premium_count, Sum(of_Prestige_Sales__c) prestige_count,
                    Sum(Added_Services__c) addedservices, Sum(Standalones__c) standalones
                    FROM Inspection__c 
                    WHERE Test_Inspection__c = false AND Status__c = 'Completed' AND Franchise__c =: acct.id
                    GROUP BY Inspector__r.FirstName
                    ORDER BY Inspector__r.FirstName];

 for(AggregateResult e: employeeInspections){
            Decimal UpgradeRate;
            Decimal PlusCount = (Decimal)e.get('plus_count');
            Decimal PremiumCount = (Decimal)e.get('premium_count');
            Decimal PrestigeCount = (Decimal)e.get('prestige_count');
            if(PlusCount == 0 &&(PrestigeCount != 0 || PremiumCount != 0)){
                    UpgradeRate = 100;
            } else if(PlusCount == 0 && PrestigeCount == 0 && PremiumCount == 0){
                    UpgradeRate = 0; 
            } else UpgradeRate = ((PremiumCount + PrestigeCount) / (PremiumCount + PrestigeCount + PlusCount)).setScale(3) * 100;
            employees.add(new FranchiseMetrics.employee(e));
            Decimal PlusCount2;
            if(PlusCount == 0){PlusCount2 = 0;}else PlusCount2 = PlusCount.setScale(0);
        }
2
  • One rarely tests a wrapper class directly. Your main test should test the results of whatever uses this wrapper class. You shouldn't be trying to induce coverage on the wrapper class directly.
    – sfdcfox
    Nov 11, 2018 at 17:06
  • Also, public Decimal PlusCount2 {get{if(PlusCount == 0){return 0;}else return PlusCount.setScale(0);}} is inefficient as compared to just public Decimal PlusCount2 { get { return PlusCount.setScale(0); } }.
    – sfdcfox
    Nov 11, 2018 at 17:08

2 Answers 2

3

You get coverage the same way you get coverage for any other line of code.

Execute the code you want to test by setting up the test environment appropriately.

You can (and probably should) have more than one test method in a unit test class, and testing if/else branches falls very nicely into this pattern (one test method to test the if block, another test method to test the else block).

A barebones example:

@isTest
static void testUpgradeRateIf(){
    // someRecord needs to be generated/created sometime before this point in the test.
    // Leaving that as an exercise for the reader
    FranciseMetrics.Employee emp = FranciseMetrics.Employee(someRecord);

    // To test the if block, we need plusCount to be 0 and prestigeCount to be non-zero
    // We can do this because these properties are public
    emp.plusCount = 0;
    emp.prestigeCount = 1;

    Test.startTest();
    Integer upgradeRate = emp.upgradeRate;
    Test.stopTest();

    // Don't forget to make assertion(s)!
    // Assertions are what make unit tests truly useful to us programmers.
    System.assertEquals(100, upgradeRate, 'upgrade rate is not what we expected');

}

@isTest
static void testUpgradeRateElseIf(){
    // someRecord needs to be generated/created sometime before this point in the test.
    // Leaving that as an exercise for the reader
    FranciseMetrics.Employee emp = FranciseMetrics.Employee(someRecord);

    /* N.B. This part of the test setup has changed from the first test*/
    // To test the else if block, we need plusCount, prestigeCount, and premiumCount
    // to all be 0
    // We can do this because these properties are public
    emp.plusCount = 0;

    // ...and set the other variables appropriately

    Test.startTest();
    Integer upgradeRate = emp.upgradeRate;
    Test.stopTest();

    // Don't forget to make assertion(s)!
    // Assertions are what make unit tests truly useful to us programmers.
}

@isTest
static void testUpgradeRateElse(){
    // someRecord needs to be generated/created sometime before this point in the test.
    // Leaving that as an exercise for the reader
    FranciseMetrics.Employee emp = FranciseMetrics.Employee(someRecord);

    /* N.B. This part of the test setup has changed from the first test*/
    // To test the else block, we need plusCount, prestigeCount, and premiumCount
    // to all be non-zero
    // We can do this because these properties are public
    emp.plusCount = 1;

    // ...and set the other variables appropriately

    Test.startTest();
    Integer upgradeRate = emp.upgradeRate;
    Test.stopTest();

    // Don't forget to make assertion(s)!
    // Assertions are what make unit tests truly useful to us programmers.
}
1
  • Thank you this was helpful. What works is this: FranchiseMetrics.employee emp = employees[0]; emp.plusCount = 0; emp.PrestigeCount = 1; Decimal UpgradeRate = emp.UpgradeRate; Decimal PlusCount2 = emp.PlusCount2;
    – Sebastian
    Nov 11, 2018 at 17:26
0

get and set properties in apex code are rough equivalents of get<VariableName>() and set<VariableName>().

In order to cover them in a unit test, you need to implicitly run these getters/setters.

E.g. for UpgradeRate variable:

@isTest
static void testUpgradeRate(){
    Employee testingEmployee = new Employee();

    //cover set
    testingEmployee.UpgradeRate = 0; 

    //cover get
    System.assertEquals(<your_expected_value>, testingEmployee.UpgradeRate); 
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .