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












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









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
    Nov 11 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 at 17:08
















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









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
    Nov 11 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 at 17:08














1












1








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









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






code-coverage wrapper-class






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 16:44









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
    Nov 11 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 at 17:08


















  • 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 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 at 17:08
















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 at 17:06




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 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 at 17:08




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 at 17:08










2 Answers
2






active

oldest

votes


















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.
}





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
    Nov 11 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);
}





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',
    autoActivateHeartbeat: false,
    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















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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.
    }





    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
      Nov 11 at 17:26


















    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.
    }





    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
      Nov 11 at 17:26
















    3












    3








    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.
    }





    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 Nov 11 at 17:13

























    answered Nov 11 at 17:05









    Derek F

    19k31849




    19k31849












    • 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 at 17:26




















    • 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 at 17:26


















    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 at 17:26






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





    share|improve this answer


























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





      share|improve this answer
























        0












        0








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





        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 Nov 11 at 17:17









        Alex Fisher

        686




        686






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Salesforce Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            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















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Full-time equivalent

            さくらももこ

            13 indicted, 8 arrested in Calif. drug cartel investigation