+ Start a Discussion
PS81PS81 

Test class for standard controller

Hello

I have a standard controller overriding the 'edit page' for opportunity. Need some help to write a test class to do a code coverage for this standard contlr please?

my overridding page :
 
<apex:page standardController="Opportunity" tabStyle="Opportunity" extensions="OpportunitySave" >
		<apex:sectionHeader title="Opportunity Edit" subtitle="{!Opportunity.name}"/>
		….
		….
		….
		<body>
		    <apex:pageBlock title="Opportunity Edit" mode="edit"  >
		        <apex:pageBlockButtons >
		            <apex:commandButton action="{!save}" value="Save" />
		            <apex:commandButton action="{!Cancel}" value="Cancel"/>
		        </apex:pageBlockButtons>
		        <apex:pageblocksection id="OprInfo" title="Opportunity Information" columns="2">
		            <apex:inputfield value="{!Opportunity.name}" id="oprname"/> 
		            <apex:inputfield value="{!Opportunity.AccountId}" id="opraccname" required="true"/> 
		….
		….
		….

and the class :
 
public with sharing class OpportunitySave {
		    public string probs {set;get;}
		    public boolean disable{set;get;}
		    public Opportunity opr {get;set;}
		    private ApexPages.StandardController stdCtrl;
		    public OpportunitySave(ApexPages.StandardController controller) {
		        stdCtrl = controller;
		        opr = (Opportunity)controller.getRecord();
		        disable = false;
		    }
		    public PageReference setpresales(){
		        if (opr.Type == 'Big Ticket')
		            opr.Presales__c = 'Yes';
		        else
		            opr.Presales__c = 'No';
		        return null;
		    }
		    public PageReference setProbability(){
		        }
		        return null;
		    }


 
Best Answer chosen by PS81
ManojjenaManojjena
Hi Ps81
Try with belwo code and add mandatory fields to the test record .
 
@isTest
			public class TestOpportunitySave{
				private static testMethod void unitTest(){
					Account acc=new Account();
					acc.name='TestAccount';
					Insert acc;
					Opportunity opp=new opportunity();
					opp.StageName='Prospecting';
					opp.CloseDate=System.Today();
					opp.Accountid=acc.id;
					insert opp;
				  ApexPages.standardControler stdCon=new  ApexPages.standardControler(opp);
				  OpportunitySave oppsave=new OpportunitySave(stdCon);
				  oppSave.setpresales();
				  oppSave.setProbability();
				 }
			}
			
For reference and concepts of test class please follow belwo link .
http://manojjena20.blogspot.in/2015/06/tips-and-tricks-for-test-class.html

Let me know if it helps !!