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
SFDC Coder 8SFDC Coder 8 

Need help in test class for StandardController

Hi All,
I am new to salesforce.
I have created a custom button in visualforce page to change status field.
I have written visualforce page and controller class for the same.
I want to write test class for the same class.

Here is my code
 
public with sharing class changeStstus {

    Public id lid;
     Public Order lRec;
     Public string strRs{get;set;}  
     public string strSt{get;set;}
    
    public changeStstus(ApexPages.StandardController controller) {
        
        if(ApexPages.currentPage().getParameters().get('id') != null) {      
              lid= ApexPages.currentPage().getParameters().get('id');
            getRecord(lid);     
        }
      }
    
    public void getRecord(ID lid){
       lRec = [select id,F1__c,status from Order where id =:lid];
       strRs = lRec.F1__c;
       strSt='Saved';
      }
    
    public PageReference  Save(){
        lRec.F1__c = strRs;
        lRec.status= strSt;
        lRec lRec;  
        PageReference ReturnPage = new PageReference('/' + lRec.id); 
        ReturnPage.setRedirect(true); 
        return ReturnPage;
    }
   
    public PageReference  Cancel(){
        PageReference ReturnPage = new PageReference('/' + lRec.id); 
        ReturnPage.setRedirect(true); 
        return ReturnPage;  
    }

}

Please help me
Thanks in Advance
Best Answer chosen by SFDC Coder 8
sfdcMonkey.comsfdcMonkey.com
hi try below test class
@isTest
public class thecontrollerTests {

    public static testMethod void testMyController() {
        account acc = new account();
        acc.Name = 'test account';
        // add all required fields
        insert acc ;
        
        
        contract cc = new contract();
        cc.AccountId = acc.Id;
        cc.Status = 'Draft';
        cc.StartDate = system.today();
        cc.ContractTerm = 2;
        // add all required fields
        insert cc;
        
        order o = new order();
        o.AccountId = acc.Id;
        o.ContractId = cc.Id;
        o.Status = 'Draft';
        o.EffectiveDate = system.today();
        // add all required fields
        insert o;
        
    PageReference incidentPage = Page.test123;
    incidentPage.getParameters().put('id', o.Id);  
    Test.setCurrentPage(incidentPage);    
    ApexPages.StandardController control = new ApexPages.Standardcontroller(o);
    changeStstus pageControl = new changeStstus(control); 
     pageControl.save();
     pageControl.Cancel();   
     
    }
}
let me inform if it helps you
thanks

 

All Answers

SFDC Coder 8SFDC Coder 8
Hi Piyush,
This is my VF page.
<apex:page standardController="Order" extensions="changeStatus" sidebar="false">

<apex:form >
<apex:pageMessages />
    <apex:pageBlock >
    
<ul class="form-style-1">
  
    <li>
        <label>Please Enter Reason for Cancel<span class="required">*</span></label><br></br>
        <apex:inputTextarea id="cancelReson"  value="{!strRS}" required="true" style="width:20%;height:100px;"/>
    </li>
    
        <apex:pageBlockButtons location="bottom">
         <apex:commandButton styleClass="buttonStyle" value="Save" action="{!Save}"/> 
        <!-- <apex:commandButton styleClass="buttonStyle"  action="{!CancelReason}" value="Cancel" immediate="true" html-formnovalidate="formnovalidate" />-->
         </apex:pageBlockButtons>
    
</ul>

 </apex:pageBlock>
</apex:form>


</apex:page>

 
SFDC Coder 8SFDC Coder 8
Hi Piyush,
I tried test as below
@isTest
public class changeStatus_Test {
 static testMethod void myAccountFilterTest() {

    order acc = new order(AccountId='001N000000ufgrte',status='saved',EffectiveDate=Date.newInstance(2008,12,31));
    insert acc;
    
     Test.StartTest();
      ApexPages.StandardController sc = new ApexPages.StandardController(acc);
      changeStatus filter = new changeStatus(sc);

     Test.StopTest();
  
    }
}

but I am able to cover only two lines.
Can you please help me in this?
Thank you
sfdcMonkey.comsfdcMonkey.com
hi try below test class
@isTest
public class thecontrollerTests {

    public static testMethod void testMyController() {
        account acc = new account();
        acc.Name = 'test account';
        // add all required fields
        insert acc ;
        
        
        contract cc = new contract();
        cc.AccountId = acc.Id;
        cc.Status = 'Draft';
        cc.StartDate = system.today();
        cc.ContractTerm = 2;
        // add all required fields
        insert cc;
        
        order o = new order();
        o.AccountId = acc.Id;
        o.ContractId = cc.Id;
        o.Status = 'Draft';
        o.EffectiveDate = system.today();
        // add all required fields
        insert o;
        
    PageReference incidentPage = Page.test123;
    incidentPage.getParameters().put('id', o.Id);  
    Test.setCurrentPage(incidentPage);    
    ApexPages.StandardController control = new ApexPages.Standardcontroller(o);
    changeStstus pageControl = new changeStstus(control); 
     pageControl.save();
     pageControl.Cancel();   
     
    }
}
let me inform if it helps you
thanks

 
This was selected as the best answer