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
Ian DoneyIan Doney 

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


   
}
 
Best Answer chosen by Ian Doney
Maharajan CMaharajan C
Hi Ian,

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.
@isTest
public class WorkOrderLineItemsOnWorkOrderClassTest {
    static testmethod void WorkOrderLineItemstest (){
        WorkOrder wo = new WorkOrder();
        wo.subject ='title';
        // Add your remaining Mandatory Fields to create the WorkOrder record
        insert wo;
        WorkOrderLineItem woli = new WorkOrderLineItem();
        woli.workOrderId = wo.Id;
        woli.description = 'abcd';
        // Add your remaining here Fields to create the WorkOrderLineItem record 
        insert woli;
        ApexPages.StandardController sc = new ApexPages.StandardController(wo);
        WorkOrderLineItemsOnWorkOrderClass woliwocls = new WorkOrderLineItemsOnWorkOrderClass(sc);        
    }
}

Thanks,
Maharajan.C

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Ian,

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
Maharajan CMaharajan C
Hi Ian,

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.
@isTest
public class WorkOrderLineItemsOnWorkOrderClassTest {
    static testmethod void WorkOrderLineItemstest (){
        WorkOrder wo = new WorkOrder();
        wo.subject ='title';
        // Add your remaining Mandatory Fields to create the WorkOrder record
        insert wo;
        WorkOrderLineItem woli = new WorkOrderLineItem();
        woli.workOrderId = wo.Id;
        woli.description = 'abcd';
        // Add your remaining here Fields to create the WorkOrderLineItem record 
        insert woli;
        ApexPages.StandardController sc = new ApexPages.StandardController(wo);
        WorkOrderLineItemsOnWorkOrderClass woliwocls = new WorkOrderLineItemsOnWorkOrderClass(sc);        
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
christhanah christhanahchristhanah christhanah
I have the same issue, thanks
 
Ian DoneyIan Doney
Thank you Maharajan.
I was able to add the rest of the required fields and passed with 100% coverage