You need to sign in to do that
Don't have an account?

Need help writing a test class for standard controller extension
I need help writing a test class for my apex code. I really don't know much about apex. But I was able to create a visualforce page and an apex class that extends the standard controller. Everything works perfectly in my sandbox. But I have no idea how to write the test class so that I can push my code into production.
Here is my code for the visualforce page:
<apex:page standardController="WorkOrder" extensions="WorkOrderLineItemsOnWorkOrderClass">
<apex:sectionHeader title="Equipment on quote number {! WorkOrder.Workorder_Name__c } for {! WorkOrder.Account.Name }"/>
<p></p>
<apex:pageblock id="WorkOrderLineItemList">
<br/>
<apex:pageBlockTable value="{!WorkOrderLineItems}" var="item">
<apex:column value="{!item.Product_Code_Display__c}"/>
<apex:column value="{!item.Description}"/>
<apex:column value="{!item.Quote_Line_Quantity_Display__c}"/>
<apex:column value="{!item.Quote_Line_Net_Total_Display__c}"/>
<apex:column value="{!item.Group_Name__c}"/>
</apex:pageBlockTable>
</apex:pageblock>
</apex:page>
And here my code for my apex class that extends the standard controller:
public class WorkOrderLineItemsOnWorkOrderClass {
public List<WorkOrderLineItem> WorkOrderLineItems{get;set;}
public WorkOrder WorkOrders {get;set;}
public WorkOrder wo {get;set;}
//Constructor
public WorkOrderLineItemsOnWorkOrderClass(ApexPages.StandardController controller) {
wo = (WorkOrder)controller.getRecord();
WorkOrders = [SELECT id FROM WorkOrder WHERE id=: wo.id LIMIT 1];
WorkOrderLineItems = [SELECT id,Group_Name__c,Product_Code_Display__c,Quote_Line_Quantity_Display__c, Description, Quote_Line_Net_Total_Display__c FROM WorkOrderLineItem WHERE WorkOrderid = :WorkOrders.id ORDER BY Quote_Line__c];
}
}
Here is my code for the visualforce page:
<apex:page standardController="WorkOrder" extensions="WorkOrderLineItemsOnWorkOrderClass">
<apex:sectionHeader title="Equipment on quote number {! WorkOrder.Workorder_Name__c } for {! WorkOrder.Account.Name }"/>
<p></p>
<apex:pageblock id="WorkOrderLineItemList">
<br/>
<apex:pageBlockTable value="{!WorkOrderLineItems}" var="item">
<apex:column value="{!item.Product_Code_Display__c}"/>
<apex:column value="{!item.Description}"/>
<apex:column value="{!item.Quote_Line_Quantity_Display__c}"/>
<apex:column value="{!item.Quote_Line_Net_Total_Display__c}"/>
<apex:column value="{!item.Group_Name__c}"/>
</apex:pageBlockTable>
</apex:pageblock>
</apex:page>
And here my code for my apex class that extends the standard controller:
public class WorkOrderLineItemsOnWorkOrderClass {
public List<WorkOrderLineItem> WorkOrderLineItems{get;set;}
public WorkOrder WorkOrders {get;set;}
public WorkOrder wo {get;set;}
//Constructor
public WorkOrderLineItemsOnWorkOrderClass(ApexPages.StandardController controller) {
wo = (WorkOrder)controller.getRecord();
WorkOrders = [SELECT id FROM WorkOrder WHERE id=: wo.id LIMIT 1];
WorkOrderLineItems = [SELECT id,Group_Name__c,Product_Code_Display__c,Quote_Line_Quantity_Display__c, Description, Quote_Line_Net_Total_Display__c FROM WorkOrderLineItem WHERE WorkOrderid = :WorkOrders.id ORDER BY Quote_Line__c];
}
}
Please try the below test class:
For WorkOrderLineItems i have inserted the record only with Work Order Id and Description.So you have to add remaining fields if you want. Also if there is any Required fields are there to create WorkOrder and WOLineItem then you must have to populate the values in below test method.
Thanks,
Maharajan.C
All Answers
I found the below link that could help you in writing a test class. Could you please check this out once.
>>https://webkul.com/blog/test-classes-in-apex-salesforce/
>> https://trailhead.salesforce.com/en/content/learn/modules/apex_testing
I hope they help.
Regards,
Anutej
Please try the below test class:
For WorkOrderLineItems i have inserted the record only with Work Order Id and Description.So you have to add remaining fields if you want. Also if there is any Required fields are there to create WorkOrder and WOLineItem then you must have to populate the values in below test method.
Thanks,
Maharajan.C
I was able to add the rest of the required fields and passed with 100% coverage