function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Christopher GoslineChristopher Gosline 

VisualForce Test Class

Hi all,

I am currently struggling to develop a test class that will provide enough coverage to enable my VisualForce page and Controller to deploy to production.

The VisualForce page has 15 textual inputs , which then upon clicking the commandbutton runs the "month" inputs throught the "calculator" controller to multiply each month's input by specific fieldswithin a separate customer object - "Utilty NMC Tariffs". Then returns the summation of these products as a result displayed at the bottom of the page.

Visual Force Page:
 
<apex:page controller="Calculator">
<apex:form >

<b>
<p>Enter the Current Month:</p>
<p><apex:inputText value="{!CurrentMonth}"/></p>

<p>Enter Utility*:</p>
<p><apex:inputText value="{!Utility}"/></p>

<p>Enter Project Class*:</p>
<p><apex:inputText value="{!Pclass}"/></p>


<p>Enter consumption per month in kWh:</p>
<p> Month 1  <apex:inputText value="{!Month1}"/></p>
<p> Month 2  <apex:inputText value="{!Month2}"/></p>
<p> Month 3  <apex:inputText value="{!Month3}"/></p>
<p> Month 4  <apex:inputText value="{!Month4}"/></p>
<p> Month 5  <apex:inputText value="{!Month5}"/></p>
<p> Month 6  <apex:inputText value="{!Month6}"/></p>
<p> Month 7  <apex:inputText value="{!Month7}"/></p>
<p> Month 8  <apex:inputText value="{!Month8}"/></p>
<p> Month 9  <apex:inputText value="{!Month9}"/></p>
<p> Month 10  <apex:inputText value="{!Month10}"/></p>
<p> Month 11  <apex:inputText value="{!Month11}"/></p>
<p> Month 12  <apex:inputText value="{!Month12}"/></p>
</b>

<apex:commandButton action="{!Calculation}" value="Show Result" styleClass="buttonStyle" style="background:LightBlue;width:150px;"/>

<p>The Estimated Total Annual Cost of Electricity is:</p>
<b><p> $ <apex:outputText value="{!result}"/></p></b> 

*If you need to change the Current Month, Utility, or Project Class Refresh Page then Change

</apex:form>
</apex:page>
Controller (Calculator):
public class Calculator{

public String Utility { get; set; }
public String Calculation { get; set; }
public string CurrentMonth{get;set;}
public double result{get;set;}
public string Pclass{get;set;}


public integer Month1{get;set;}
public integer Month2{get;set;}
public integer Month3{get;set;}
public integer Month4{get;set;}
public integer Month5{get;set;}
public integer Month6{get;set;}
public integer Month7{get;set;}
public integer Month8{get;set;}
public integer Month9{get;set;}
public integer Month10{get;set;}
public integer Month11{get;set;}
public integer Month12{get;set;}

list<decimal> NMClist1 = new list<decimal>();
list<decimal> NMClist2 = new list<decimal>();
list<decimal> NMClist3 = new list<decimal>();
list<decimal> NMClist4 = new list<decimal>();
list<decimal> NMClist5 = new list<decimal>();
list<decimal> NMClist6 = new list<decimal>();
list<decimal> NMClist7 = new list<decimal>();
list<decimal> NMClist8 = new list<decimal>();
list<decimal> NMClist9 = new list<decimal>();
list<decimal> NMClist10 = new list<decimal>();
list<decimal> NMClist11 = new list<decimal>();
list<decimal> NMClist12 = new list<decimal>();
list<decimal> NMClist13 = new list<decimal>();
list<decimal> NMClist14 = new list<decimal>();
list<decimal> NMClist15 = new list<decimal>();
list<decimal> NMClist16 = new list<decimal>();
list<decimal> NMClist17 = new list<decimal>();
list<decimal> NMClist18 = new list<decimal>();
list<decimal> NMClist19 = new list<decimal>();
list<decimal> NMClist20 = new list<decimal>();
list<decimal> NMClist21 = new list<decimal>();
list<decimal> NMClist22 = new list<decimal>();
list<decimal> NMClist23 = new list<decimal>();
list<decimal> NMClist24 = new list<decimal>();

public void calculation()
{

for(utility_nmc_tariff__c NMCquery : [ SELECT January_previous_year__c, january__c,February_previous_year__c,February__c, March_previous_year__c,March__c,April_previous_year__c,April__c,May_previous_year__c, May__c, june_previous_year__c, june__c, july_previous_year__c, july__c, august_previous_year__c, august__c, september_previous_year__c, september__c, october_previous_year__c, october__c, november_previous_year__c, november__c, december_previous_year__c, december__c FROM utility_nmc_tariff__c WHERE Utility__c = : Utility AND Class__c = : pclass ])
{
    NMClist1.add(NMCquery.January_previous_year__c);
    
    NMClist2.add(NMCquery.January__c);
    
    NMClist3.add(NMCquery.february_previous_year__c);
    
    NMClist4.add(NMCquery.february__c);
    
    NMClist5.add(NMCquery.march_previous_year__c);
    
    NMClist6.add(NMCquery.march__c);
    
    NMClist7.add(NMCquery.april_previous_year__c);
    
    NMClist8.add(NMCquery.april__c);
   
    NMClist9.add(NMCquery.may_previous_year__c);
    
    NMClist10.add(NMCquery.may__c);
    
    NMClist11.add(NMCquery.June_previous_year__c);
    
    NMClist12.add(NMCquery.June__c);
    
    NMClist13.add(NMCquery.July_previous_year__c);
    
    NMClist14.add(NMCquery.July__c);
    
    NMClist15.add(NMCquery.August_previous_year__c);
    
    NMClist16.add(NMCquery.August__c);
    
    NMClist17.add(NMCquery.September_previous_year__c);
    
    NMClist18.add(NMCquery.September__c);
    
    NMClist19.add(NMCquery.October_previous_year__c);
    
    NMClist20.add(NMCquery.October__c);
    
    NMClist21.add(NMCquery.November_previous_year__c);
    
    NMClist22.add(NMCquery.November__c);
    
    NMClist23.add(NMCquery.December_previous_year__c);
    
    NMClist24.add(NMCquery.December__c);
    
    }
    
 


decimal j1 = NMCList1.get(0);
decimal j2 = NMCList2.get(0);
decimal f1 = NMCList3.get(0);
decimal f2 = NMCList4.get(0);
decimal m1 = NMCList5.get(0);
decimal m2 = NMCList6.get(0);
decimal a1 = NMCList7.get(0);
decimal a2 = NMCList8.get(0);
decimal my1 = NMCList9.get(0);
decimal my2 = NMCList10.get(0);
decimal june1 = NMCList11.get(0);
decimal june2 = NMCList12.get(0);
decimal july1 = NMCList13.get(0);
decimal july2 = NMCList14.get(0);
decimal aug1 = NMCList15.get(0);
decimal aug2 = NMCList16.get(0);
decimal sep1 = NMCList17.get(0);
decimal sep2 = NMCList18.get(0);
decimal oct1 = NMCList19.get(0);
decimal oct2 = NMCList20.get(0);
decimal nov1 = NMCList21.get(0);
decimal nov2 = NMCList22.get(0);
decimal dec1 = NMCList23.get(0);
decimal dec2 = NMCList24.get(0);


if(CurrentMonth == 'January'){
result= 
month1 * j1 +
month2 * f1 +
month3 * m1 +
month4 * a1 +
month5 * my1 +
month6 * june1 +
month7 * july1 +
month8 * aug1 +
month9 * sep1 +
month10 * oct1 +
month11 * nov1 +
month12 * dec1;
}
if(CurrentMonth == 'February'){
result= 
month1 * f1 +
month2 * m1 +
month3 * a1 +
month4 * my1 +
month5 * june1 +
month6 * july1 +
month7 * aug1 +
month8 * sep1 +
month9 * oct1 +
month10 * nov1 +
month11 * dec1 +
month12 * j2;
}
if(CurrentMonth == 'March'){
result= 
month1 * m1 +
month2 * a1 +
month3 * my1 +
month4 * june1 +
month5 * july1 +
month6 * aug1 +
month7 * sep1 +
month8 * oct1 +
month9 * nov1 +
month10 * dec1 +
month11 * j2 +
month12 * f2;
}
if(CurrentMonth == 'April'){
result= 
month1 * a1 +
month2 * my1 +
month3 * june1 +
month4 * july1 +
month5 * aug1 +
month6 * sep1 +
month7 * oct1 +
month8 * nov1 +
month9 * dec1 +
month10 * j2 +
month11 * f2 +
month12 * m2;
}

if(CurrentMonth == 'May'){
result= 
month1 * my1 +
month2 * june1 +
month3 * july1 +
month4 * aug1 +
month5 * sep1 +
month6 * oct1 +
month7 * nov1 +
month8 * dec1 +
month9 * j2 +
month10 * f2 +
month11 * m2 +
month12 * a2;
}
if(CurrentMonth == 'June'){

result= 
month1 * june1 +
month2 * july1 +
month3 * aug1 +
month4 * sep1 +
month5 * oct1 +
month6 * nov1 +
month7 * dec1 +
month8 * j2 +
month9 * f2 +
month10 * m2 +
month12 * a2+
month12 * my2 ;
}
if(CurrentMonth == 'July'){

result= 
month1 * july1 +
month2 * aug1 +
month3 * sep1 +
month4 * oct1 +
month5 * nov1 +
month6 * dec1 +
month7 * j2 +
month8 * f2 +
month9 * m2 +
month10 * a2+
month11 * my2 +
month12 * june2 ;
}
if(CurrentMonth == 'August'){
result= 
month1 * aug1 +
month2 * sep1 +
month3 * oct1 +
month4 * nov1 +
month5 * dec1 +
month6 * j2 +
month7 * f2 +
month8 * m2 +
month9 * a2+
month10 * my2 +
month11 * june2 +
month12 * july2;

}

if(CurrentMonth == 'September'){
result= 
month1 * sep1 +
month2 * oct1 +
month3 * nov1 +
month4 * dec1 +
month5 * j2 +
month6 * f2 +
month7 * m2 +
month8 * a2+
month9 * my2 +
month10 * june2 +
month11 * july2 +
month12 * aug2;
}

if(CurrentMonth == 'October'){
result= 

month1 * oct1 +
month2 * nov1 +
month3 * dec1 +
month4 * j2 +
month5 * f2 +
month6 * m2 +
month7 * a2+
month8 * my2 +
month9 * june2 +
month10 * july2 +
month11 * aug2 +
month12 * sep2;
}
if(CurrentMonth == 'November'){
result= 
month1 * nov1 +
month2 * dec1 +
month3 * j2 +
month4 * f2 +
month5 * m2 +
month6 * a2+
month7 * my2 +
month8 * june2 +
month9 * july2 +
month10 * aug2 +
month11 * sep2 +
month12 * oct2;
}

if(CurrentMonth == 'December'){
result= 
month1 * dec1 +
month2 * j2 +
month3 * f2 +
month4 * m2 +
month5 * a2+
month6 * my2 +
month7 * june2 +
month8 * july2 +
month9 * aug2 +
month10 * sep2 +
month11 * oct2 +
month12 * nov2;

}




}
}

The calculation is simple and I have it working in the Sandbox. I just have to deploy it. My test class is as follows, it achieves 15% coverage. I am truly unsure how to construct this. I have authored APEX Trigger Test Classes multiple times; however this is a bit different. Keep in mind I coud be way off base on this Test class. 
 
@isTest

Public class CalculatorTest{

private static string utility = 'National Grid';
private static string pclass = 'Class 2';
private static string currentMonth = 'January';

Utility_NMC_Tariff__c NMCtariff = new Utility_NMC_Tariff__c(december__c = 1, november__c = 1, october__c = 1, september__c = 1, august__c = 1, july__c = 1, june__c = 1, may__c = 1, april__c = 1, march__c = 1, february__c = 1, january__c = 1, name = 'National Grid NEMA Class 2', utility__c = 'National Grid', class__c = 'Class 2', january_previous_year__c = 1, february_previous_year__c = 1, march_previous_year__c = 1, april_previous_year__c = 1, may_previous_year__c = 1, june_previous_year__c = 1, july_previous_year__c = 1, august_previous_year__c = 1, september_previous_year__c = 1, october_previous_year__c = 1,november_previous_year__c = 1, december_previous_year__c = 1);   

Public Static testMethod void testCalculator(){
PageReference pageRef = new PageReference('/apex/Annual_Electricity_Cost_Calculator');
Test.setCurrentPage(pageRef);


calculator newCalc = new calculator();

newCalc.Utility = 'National Grid';
newCalc.pclass = 'Class 2';
newCalc.currentMonth = 'January';
newCalc.month1 = 1;
newCalc.month2 = 1;
newCalc.month3 = 1;
newCalc.month4 = 1;
newCalc.month5 = 1;
newCalc.month6 = 1;
newCalc.month7 = 1;
newCalc.month8 = 1;
newCalc.month9 = 1;
newCalc.month10 = 1;
newCalc.month11= 1;
newCalc.month12 = 1;

string nextPage = system.currentPageReference().getUrl();

System.assertEquals('/apex/Annual_Electricity_Cost_Calculator', nextPage);

}
}



Any help would be greatly appreciated.

Also, if you noticed at the end of the VF page I have a note that instructs the user to refresh the page if they input a month or utility click the command button and then want to change this variable. Currently, if you do so the controller holds on to the old variables and doesn not react to the new ones. Any suggestions. This problem is minor. 

Thank you.
 
Vipul TaylorVipul Taylor
Hi Christopher,

This is because your current test class is only accounting for 'January' as the currentMonth. Hence all your if blocks for the remaining months are getting skipped.

Vipul
Amit Chaudhary 8Amit Chaudhary 8
Please try below code. I hope that will help u
@isTest

Public class CalculatorTest
{

	private static string utility = 'National Grid';
	private static string pclass = 'Class 2';
	private static string currentMonth = 'January';

	Utility_NMC_Tariff__c NMCtariff = new Utility_NMC_Tariff__c(december__c = 1, november__c = 1, october__c = 1, september__c = 1, august__c = 1, july__c = 1, june__c = 1, may__c = 1, april__c = 1, march__c = 1, february__c = 1, january__c = 1, name = 'National Grid NEMA Class 2', utility__c = 'National Grid', class__c = 'Class 2', january_previous_year__c = 1, february_previous_year__c = 1, march_previous_year__c = 1, april_previous_year__c = 1, may_previous_year__c = 1, june_previous_year__c = 1, july_previous_year__c = 1, august_previous_year__c = 1, september_previous_year__c = 1, october_previous_year__c = 1,november_previous_year__c = 1, december_previous_year__c = 1);   

	Public Static testMethod void testCalculator()
	{

		PageReference pageRef = new PageReference('/apex/Annual_Electricity_Cost_Calculator');
		Test.setCurrentPage(pageRef);


		calculator newCalc = new calculator();

		newCalc.Utility = 'National Grid';
		newCalc.pclass = 'Class 2';
		newCalc.currentMonth = 'January';
		newCalc.month1 = 1;
		newCalc.month2 = 1;
		newCalc.month3 = 1;
		newCalc.month4 = 1;
		newCalc.month5 = 1;
		newCalc.month6 = 1;
		newCalc.month7 = 1;
		newCalc.month8 = 1;
		newCalc.month9 = 1;
		newCalc.month10 = 1;
		newCalc.month11= 1;
		newCalc.month12 = 1;
		newCalc.calculation();
		newCalc.currentMonth = 'February';
		newCalc.calculation();

		newCalc.currentMonth = 'March';
		newCalc.calculation();
		
		newCalc.currentMonth = 'April';
		newCalc.calculation();

		newCalc.currentMonth = 'May';
		newCalc.calculation();

		newCalc.currentMonth = 'June';
		newCalc.calculation();

		newCalc.currentMonth = 'July';
		newCalc.calculation();

		newCalc.currentMonth = 'August';
		newCalc.calculation();

		newCalc.currentMonth = 'September';
		newCalc.calculation();
		
		newCalc.currentMonth = 'October';
		newCalc.calculation();

		newCalc.currentMonth = 'November';
		newCalc.calculation();

		newCalc.currentMonth = 'December';
		newCalc.calculation();

		string nextPage = system.currentPageReference().getUrl();

		System.assertEquals('/apex/Annual_Electricity_Cost_Calculator', nextPage);

	}
}

Please check below blog for test class i hope that will help u
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html

Please follow below salesforce Best Practice for Test Classes :-

1. Test class must start with @isTest annotation if class class version is more than 25
2. Test environment support @testVisible , @testSetUp as well
3. Unit test is to test particular piece of code working properly or not .
4. Unit test method takes no argument ,commit no data to database ,send no email ,flagged with testMethod keyword .
5. To deploy to production at-least 75% code coverage is required 
6. System.debug statement are not counted as a part of apex code limit.
7. Test method and test classes are not counted as a part of code limit
9. We should not focus on the  percentage of code coverage ,we should make sure that every use case should covered including positive, negative,bulk and single record .
Single Action -To verify that the the single record produces the correct an expected result .
Bulk action -Any apex record trigger ,class or extension must be invoked for 1-200 records .
Positive behavior : Test every expected behavior occurs through every expected permutation , i,e user filled out every correctly data and not go past the limit .
Negative Testcase :-Not to add future date , Not to specify negative amount.
Restricted User :-Test whether a user with restricted access used in your code .10. Test class should be annotated with @isTest .
11 . @isTest annotation with test method  is equivalent to testMethod keyword .
12. Test method should static and no void return type .
13. Test class and method default access is private ,no matter to add access specifier .
14. classes with @isTest annotation can't be a interface or enum .
15. Test method code can't be invoked by non test request .
16. Stating with salesforce API 28.0 test method can not reside inside non test classes .
17. @Testvisible annotation to make visible private methods inside test classes.
18. Test method can not be used to test web-service call out . Please use call out mock .
19. You can't  send email from test method.
20.User, profile, organization, AsyncApexjob, Corntrigger, RecordType, ApexClass, ApexComponent ,ApexPage we can access without (seeAllData=true) .
21. SeeAllData=true will not work for API 23 version eailer .
22. Accessing static resource test records in test class e,g List<Account> accList=Test.loadData(Account,SobjectType,'ResourceName').
23. Create TestFactory class with @isTest annotation to exclude from organization code size limit .
24. @testSetup to create test records once in a method  and use in every test method in the test class .
25. We can run unit test by using Salesforce Standard UI,Force.com IDE ,Console ,API.
26. Maximum number of test classes run per 24 hour of period is  not grater of 500 or 10 multiplication of test classes of your organization.
27. As apex runs in system mode so the permission and record sharing are not taken into account . So we need to use system.runAs to enforce record sharing .
28. System.runAs will not enforce user permission or field level permission .
29. Every test to runAs count against the total number of DML issued in the process .


Please let us know if this post will help you
Christopher GoslineChristopher Gosline
Doesn't seem to work. The code keeps getting hung up on the Controller's query: 


Class.Calculator.calculation: line 106, column 1
Class.CalculatorTest.testCalculator: line 54, column 1
 
for(utility_nmc_tariff__c NMCquery : [ 
SELECT January_previous_year__c, 
january__c,
February_previous_year__c,
February__c, 
March_previous_year__c,
March__c,
April_previous_year__c,
April__c,
May_previous_year__c, 
May__c, 
june_previous_year__c, 
june__c, 
july_previous_year__c, 
july__c, 
august_previous_year__c, 
august__c, 
september_previous_year__c, 
september__c, 
october_previous_year__c, 
october__c, 
november_previous_year__c, 
november__c, 
december_previous_year__c, 
december__c 
FROM utility_nmc_tariff__c 
WHERE Utility__c = : Utility AND Class__c = : pclass ])

As you can see the WHERE statement requires the "Utility__c" and "Class__c" fields to be equal to the inputText lines in the VF page. I feel as if this is where the query is getting hung up. I am not positive that my code is assigning these inputText lines properly so that they query the instantiated "Utility_NMC_Tariff" record in the controller.

I'm not experienced with using a binder variable in the WHERE statement. I now it works because evrything works but the test class. Just not sure how to set these binder variables to the inputText lines.

 
Christopher GoslineChristopher Gosline
Sorry, the error being shown above is 


System.ListException: List index out of bounds: 0

Class.Calculator.calculation: line 106, column 1
Class.CalculatorTest.testCalculator: line 54, column 1

Which from my understand means that the query is returning no results.

Again, thank you for your help