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
Harvey2016Harvey2016 

Can someone guide me to write a test class for the below trigger .

This is my VF Page
<apex:page StandardController="VM_Sales_Order__c" extensions="Reallocation_OrderClass">
    <apex:form >
        <apex:pageMessages id="errorMessage" />
        <apex:pageBlock title="Order Reallocation Page" id="abc">
            <apex:pageBlockButtons >        
                <apex:commandButton value="Save" action="{!updateRec}" / >
                    <apex:commandButton value="Cancel" action="{!cancelRec}" immediate="true"/>
                </apex:pageBlockButtons>
                <apex:pageBlockSection title="Update">
                    <apex:outputField value="{!vmo.Name}"/>
                    <apex:inputField value="{!vmo.Reallocation_Territory__c}" label="Reallocation Territory" >
                        <apex:actionSupport event="onchange" action="{!getTerrDetails}" reRender="abc,vms" status="actnstatus" />
                    </apex:inputField>
                    <apex:outputField value="{!vmo.Sales_Territory__c}"/>
                    <apex:outputField value="{!vmo.Shipping_Address__c}"/>
                    <apex:outputField value="{!vmo.Shipping_Address__r.SoldInCountry__c}"/>
                    
                    <apex:inputField value="{!vmo.VM_SalesDescription__c}" />
                    
                    
                </apex:pageBlockSection>
            </apex:pageBlock>
            <apex:outputPanel >
                <apex:actionstatus id="actnstatus">
                    <apex:facet name="start">
                        <div class="waitingSearchDiv" id="el_loading" style="background-color: #fbfbfb; height: 100%;opacity:0.55;width:100%;">                                                                         
                            <div class="waitingHolder" style="top: 74.2px; width: 91px;">
                                <img class="waitingImage" src="/img/loading.gif" title="Please Wait..." />
                                <span class="waitingDescription">Please Wait...</span>
                            </div>
                        </div>
                    </apex:facet>
                </apex:actionstatus>
            </apex:outputpanel>
        </apex:form> 
    </apex:page

My extension Class


public class Reallocation_OrderClass {    
    public id vmID;
    public Territory__c Terr{get;set;}
    public VM_Sales_Order__c vmo{get;set;}
    public List<Order_Reallocation__c > ord= new List<Order_Reallocation__c >();    
    public List<Order_Reallocation__c> updateList = new List<Order_Reallocation__c>();
    set<Id> oldTerrId =  New set<Id>();
    set<Id> newTerrId =  New set<Id>();
    set<String> oldTerrDes =  New set<String>();
    set<String> newTerrDes =  New set<String>();
    public Reallocation_OrderClass(ApexPages.StandardController controller) {
        vmo= (VM_Sales_Order__c)controller.getRecord();
        vmID=ApexPages.currentPage().getParameters().get('id');
        vmo =[select  Name,Sales_Territory__c,Reallocation_Territory__c,Shipping_Address__c,Shipping_Address__r.SoldInCountry__c,VM_SalesDescription__c from VM_Sales_Order__c where id=:vmID]; 
        oldTerrId.add(vmo.Reallocation_Territory__c);
        oldTerrDes.add(vmo.VM_SalesDescription__c);
        System.debug('Old vmo Reallocation_Territory'+ vmo.Reallocation_Territory__c);
    }
    public void getTerrDetails()
    {
        
        if(vmo.Reallocation_Territory__c!= Null){
            System.debug('New vmo Reallocation_Territory'+ vmo.Reallocation_Territory__c);
            Terr=[select id,name from Territory__c where id=:vmo.Reallocation_Territory__c];
            vmo.Sales_Territory__c=Terr.name;    
        }                
    }
    public pageReference updateRec(){
        newTerrId.add(vmo.Reallocation_Territory__c); 
        newTerrDes.add(vmo.VM_SalesDescription__c);
        if(vmo.VM_SalesDescription__c==null)
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info,'Description field is mandatory to save the record.')); 
        }
        else{
            update vmo;   
            if(oldTerrId!=newTerrId || oldTerrDes!=newTerrDes)                   
            {        
                ord=[select id,VM_Sales_Order__c,SFDC_Order_Id__c,SoldInCountry__c,SoldInTerritory__c,Order_realloc_Description__c from Order_Reallocation__c where 
                     VM_Sales_Order__c =:vmo.id ];    
                
                if(ord.isEmpty())        
                {
                    Order_Reallocation__c   ordRec = new Order_Reallocation__c();
                    ordRec .VM_Sales_Order__c = vmid;
                    ordRec .SFDC_Order_Id__c= vmo.Name;
                    ordRec .SoldInCountry__c= vmo.Shipping_Address__r.SoldInCountry__c;    
                    ordRec .SoldInTerritory__c= vmo.Reallocation_Territory__c;
                    ordRec .Order_realloc_Description__c =vmo.VM_SalesDescription__c;        
                    insert ordRec ; 
                }                              
                else {    
                    for(Order_Reallocation__c  upord :ord)
                    {
                        upord .VM_Sales_Order__c = vmid;
                        upord .SFDC_Order_Id__c= vmo.Name;
                        upord .SoldInCountry__c= vmo.Shipping_Address__r.SoldInCountry__c;    
                        upord .SoldInTerritory__c= vmo.Reallocation_Territory__c;
                        upord .Order_realloc_Description__c =vmo.VM_SalesDescription__c;        
                        update upord;  
                        updateList.add(upord);
                    }  
                    update updateList;
                }
                
                
            }    
            return new pagereference('/'+vmID);
        }
        return null;
    }     
    public pageReference cancelRec(){
        return new pagereference('/'+vmID);
    }
}
pconpcon
The contents of the Visualforce page does not matter as your test will not interact with the page.  The only time the Page even comes into a test is if you are pulling content out of the current page reference.  To test your controller, you do the same thing you do for any other test but you will simply create a standardController object to pass in.  I recommend reading over this article [1] to learn how to do your controller test.  As well as these articles [2] [3] [4] [5] for additional reading on tests in general.  You will need to create a new instance of your Reallocation_OrderClass and then call your cancelRec and updateRec method from that instance.

[1] https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm
[2] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[3] http://pcon.github.io/presentations/testing/
[4] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
[5] http://blog.deadlypenguin.com/blog/testing/strategies/