+ Start a Discussion
sachitanand kumarsachitanand kumar 

how to write test class for standard controller extension for the below code??? Please help me.... i am new at salesforce

Here is my apex class.
public class AccountOwnerUpdate{
    public Boolean isSelected{get;set;}
    public Account accounts{get;set;}
    public List<Account> selectedAccounts{get;set;}
   // public Boolean sendEmail{get;set;}
    public AccountOwnerUpdate(ApexPages.StandardSetController standardController)
    {   
        if(standardController.getSelected().size() == 0){
            isSelected = false;
            ApexPages.Message msg = new ApexPages.message(ApexPages.Severity.ERROR,'No Account Selected');
            ApexPages.addMessage(msg);
        }else{
            isSelected = true;
            selectedAccounts=  [SELECT id,name,ownerid from Account where id=:standardController.getSelected()];
           accounts = selectedAccounts[0]; 
           // accounts.ownerid = null; 
       }       
    }
    public PageReference updateAccount()
    {   
        for(Account a: selectedAccounts){
            a.ownerid = accounts.ownerid;
        }
        update selectedAccounts;
        return new Pagereference('/001/o');
    }

}

Here is my visual force page code. 

<apex:page standardController="Account" recordsetvar="Account" tabStyle="Account" extensions="AccountOwnerUpdate">
    <apex:pagemessages />
    <script>
        function myFunction() {      
            alert("Are you sure?");
        }
    </script>

    <apex:form rendered="{!isSelected}">
        <apex:sectionHeader subtitle="Change Owner" title="Account Edit" description="This screen allows you to transfer ownership of a Account to another user. When you transfer ownership of a Account, the new owner will own:
<br/>• all notes and attachments recorded for the current owner
<br/>• all open activities (tasks and events) owned by the current owner
<br/>
<br/>Note that completed activities and open activities owned by other users will not be transferred."/>
        <apex:pageblock mode="Edit" title="Account Edit">
            
    
           <apex:pageblockSection title="Select New Owner">
               <apex:inputField value="{!accounts.OwnerId}"/>
            </apex:pageblockSection>
           
            <apex:pageblockButtons >
                <apex:commandButton value="Save" action="{!updateAccount}" rendered="{!$User.Email == 'raj.sharma@theeclgroup.com'}"/>
                <apex:actionRegion >
                    <apex:commandButton value="Cancel" onclick="myFunction()" action="{!cancel}"/>  
               
                </apex:actionRegion>
            </apex:pageblockButtons>
        </apex:pageblock>
    </apex:form>
</apex:page>
sagarika bsagarika b
Hi Kumar,

When writing unit tests for controller extension and custom controller classes, you can set query parameters that can then be used in the tests.

Please check the below link once
sachitanand kumarsachitanand kumar
sorry i can't understand.
can you please help me how can write test class for above apex class..??