How to get code coverage on formula fields in a wrapper class

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












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 PlusCountget;set;
public Decimal PlusCount2 getif(PlusCount == 0)return 0;else return PlusCount.setScale(0);
public Decimal PremiumCountget;set;
public Decimal PremiumCount2 getif(PremiumCount == 0)return 0;else return PremiumCount.setScale(0);
public Decimal PrestigeCountget;set;
public Decimal PrestigeCount2 getif(PrestigeCount == 0)return 0;else return PrestigeCount.setScale(0);
public Decimal UpgradeRate
get


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) 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);










share|improve this question





















  • 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
    6 hours ago










  • Also, public Decimal PlusCount2 getif(PlusCount == 0)return 0;else return PlusCount.setScale(0); is inefficient as compared to just public Decimal PlusCount2 get return PlusCount.setScale(0); .
    – sfdcfox
    6 hours ago
















up vote
1
down vote

favorite












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 PlusCountget;set;
public Decimal PlusCount2 getif(PlusCount == 0)return 0;else return PlusCount.setScale(0);
public Decimal PremiumCountget;set;
public Decimal PremiumCount2 getif(PremiumCount == 0)return 0;else return PremiumCount.setScale(0);
public Decimal PrestigeCountget;set;
public Decimal PrestigeCount2 getif(PrestigeCount == 0)return 0;else return PrestigeCount.setScale(0);
public Decimal UpgradeRate
get


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) 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);










share|improve this question





















  • 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
    6 hours ago










  • Also, public Decimal PlusCount2 getif(PlusCount == 0)return 0;else return PlusCount.setScale(0); is inefficient as compared to just public Decimal PlusCount2 get return PlusCount.setScale(0); .
    – sfdcfox
    6 hours ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











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 PlusCountget;set;
public Decimal PlusCount2 getif(PlusCount == 0)return 0;else return PlusCount.setScale(0);
public Decimal PremiumCountget;set;
public Decimal PremiumCount2 getif(PremiumCount == 0)return 0;else return PremiumCount.setScale(0);
public Decimal PrestigeCountget;set;
public Decimal PrestigeCount2 getif(PrestigeCount == 0)return 0;else return PrestigeCount.setScale(0);
public Decimal UpgradeRate
get


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) 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);










share|improve this question













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 PlusCountget;set;
public Decimal PlusCount2 getif(PlusCount == 0)return 0;else return PlusCount.setScale(0);
public Decimal PremiumCountget;set;
public Decimal PremiumCount2 getif(PremiumCount == 0)return 0;else return PremiumCount.setScale(0);
public Decimal PrestigeCountget;set;
public Decimal PrestigeCount2 getif(PrestigeCount == 0)return 0;else return PrestigeCount.setScale(0);
public Decimal UpgradeRate
get


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) 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);







code-coverage wrapper-class






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 6 hours ago









Sebastian

415




415











  • 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
    6 hours ago










  • Also, public Decimal PlusCount2 getif(PlusCount == 0)return 0;else return PlusCount.setScale(0); is inefficient as compared to just public Decimal PlusCount2 get return PlusCount.setScale(0); .
    – sfdcfox
    6 hours ago
















  • 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
    6 hours ago










  • Also, public Decimal PlusCount2 getif(PlusCount == 0)return 0;else return PlusCount.setScale(0); is inefficient as compared to just public Decimal PlusCount2 get return PlusCount.setScale(0); .
    – sfdcfox
    6 hours ago















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
6 hours ago




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
6 hours ago












Also, public Decimal PlusCount2 getif(PlusCount == 0)return 0;else return PlusCount.setScale(0); is inefficient as compared to just public Decimal PlusCount2 get return PlusCount.setScale(0); .
– sfdcfox
6 hours ago




Also, public Decimal PlusCount2 getif(PlusCount == 0)return 0;else return PlusCount.setScale(0); is inefficient as compared to just public Decimal PlusCount2 get return PlusCount.setScale(0); .
– sfdcfox
6 hours ago










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










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.






share|improve this answer






















  • 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
    6 hours ago


















up vote
0
down vote













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);






share|improve this answer




















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "459"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f238975%2fhow-to-get-code-coverage-on-formula-fields-in-a-wrapper-class%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    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.






    share|improve this answer






















    • 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
      6 hours ago















    up vote
    3
    down vote



    accepted










    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.






    share|improve this answer






















    • 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
      6 hours ago













    up vote
    3
    down vote



    accepted







    up vote
    3
    down vote



    accepted






    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.






    share|improve this answer














    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 6 hours ago

























    answered 6 hours ago









    Derek F

    18.5k31646




    18.5k31646











    • 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
      6 hours ago

















    • 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
      6 hours ago
















    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
    6 hours ago





    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
    6 hours ago













    up vote
    0
    down vote













    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);






    share|improve this answer
























      up vote
      0
      down vote













      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);






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        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);






        share|improve this answer












        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);







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 6 hours ago









        Alex Fisher

        686




        686



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f238975%2fhow-to-get-code-coverage-on-formula-fields-in-a-wrapper-class%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay