How to get code coverage on formula fields in a wrapper class
Clash 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);
code-coverage wrapper-class
add a comment |
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);
code-coverage wrapper-class
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 justpublic Decimal PlusCount2 get return PlusCount.setScale(0);
.
– sfdcfox
6 hours ago
add a comment |
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);
code-coverage wrapper-class
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
code-coverage wrapper-class
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 justpublic Decimal PlusCount2 get return PlusCount.setScale(0);
.
– sfdcfox
6 hours ago
add a comment |
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 justpublic 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
add a comment |
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.
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
add a comment |
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);
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
answered 6 hours ago
Alex Fisher
686
686
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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 justpublic Decimal PlusCount2 get return PlusCount.setScale(0);
.– sfdcfox
6 hours ago