You need to sign in to do that
Don't have an account?
Rajendra Shukla 22
hi i am trying write test for a controller class but not getting any idea pleas help me with any example how? different it is from writing test class for trigger
hear is my controller class--------------------->
public class actionFunctionController {
public Position__c poc{get;set;}
public Boolean show{get;set;}
public Boolean showmessage{get;set;}
public actionFunctionController(ApexPages.StandardController stdController){
poc=[select id,name,Hire_By__c,Hiring_Manager__c,Job_Level__c,Functional_Area__c,Job_Description__c,Location__c,Max_Pay__c,Min_Pay__c,Open_Date__c,Responsibilities__c,Travel_Required__c,Type__c,Status__c,Cancellation_Reason__c from Position__c where id=:ApexPages.currentPage().getParameters().get('id')];
show = false;
}
public PageReference Savefun(){
try{
if(poc.Status__c =='Cancelled' && poc.Cancellation_Reason__c == null){
showmessage = true;
return null;
}
else{
update poc;
showmessage=false;
PageReference newPage = new PageReference('https://ap2.salesforce.com/'+ApexPages.currentPage().getParameters().get('id'));
return newPage;
}
}
catch(Exception e){
ApexPages.addMessages(e);
return null;
}
return null;
}
public PageReference priorityChanged(){
if(poc.Status__c == 'Cancelled'){
show = true;
}
else{
show = false;
}
return null;
}
}
vf page--------------->
<apex:page standardController="Position__c" extensions="actionFunctionController">
<style>
.myCustomMessage .message {color: Red !important;}
</style>
<apex:form >
<apex:pageBlock title="{!$User.FirstName}">
<apex:pageBlockButtons >
<apex:commandButton value="sAVE" action="{!Savefun}"/>
<apex:commandButton value="cANCEL" action="{!cancel}" />
</apex:pageBlockButtons>
<apex:actionFunction name="priorityChangedJavaScript" action="{!priorityChanged}" rerender="out"/>
<apex:pageBlockSection title="Please Enter The Changes" columns="1" id="out" collapsible="false">
<apex:inputField value="{!poc.name}"/>
<apex:inputField value="{!poc.Hire_By__c}"/>
<apex:inputField value="{!poc.Hiring_Manager__c}"/>
<apex:inputField value="{!poc.Job_Level__c}"/>
<apex:inputField value="{!poc.Functional_Area__c}"/>
<apex:inputField value="{!poc.Job_Description__c}"/>
<apex:inputField value="{!poc.Location__c}"/>
<apex:inputField value="{!poc.Max_Pay__c}"/>
<apex:inputField value="{!poc.Min_Pay__c}"/>
<apex:inputField value="{!poc.Open_Date__c}"/>
<apex:inputField value="{!poc.Responsibilities__c}"/>
<apex:inputField value="{!poc.Travel_Required__c}"/>
<apex:inputField value="{!poc.Type__c}"/>
<apex:inputField value="{!poc.Status__c}" onchange="priorityChangedJavaScript()"/>
<apex:inputField value="{!poc.Cancellation_Reason__c}" rendered="{!show}"/>
<apex:outputPanel styleClass="myCustomMessage" rendered="{!showmessage}">
<apex:pageMessage severity="error" strength="1" summary="Please enter the reason" />
</apex:outputPanel>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
public class actionFunctionController {
public Position__c poc{get;set;}
public Boolean show{get;set;}
public Boolean showmessage{get;set;}
public actionFunctionController(ApexPages.StandardController stdController){
poc=[select id,name,Hire_By__c,Hiring_Manager__c,Job_Level__c,Functional_Area__c,Job_Description__c,Location__c,Max_Pay__c,Min_Pay__c,Open_Date__c,Responsibilities__c,Travel_Required__c,Type__c,Status__c,Cancellation_Reason__c from Position__c where id=:ApexPages.currentPage().getParameters().get('id')];
show = false;
}
public PageReference Savefun(){
try{
if(poc.Status__c =='Cancelled' && poc.Cancellation_Reason__c == null){
showmessage = true;
return null;
}
else{
update poc;
showmessage=false;
PageReference newPage = new PageReference('https://ap2.salesforce.com/'+ApexPages.currentPage().getParameters().get('id'));
return newPage;
}
}
catch(Exception e){
ApexPages.addMessages(e);
return null;
}
return null;
}
public PageReference priorityChanged(){
if(poc.Status__c == 'Cancelled'){
show = true;
}
else{
show = false;
}
return null;
}
}
vf page--------------->
<apex:page standardController="Position__c" extensions="actionFunctionController">
<style>
.myCustomMessage .message {color: Red !important;}
</style>
<apex:form >
<apex:pageBlock title="{!$User.FirstName}">
<apex:pageBlockButtons >
<apex:commandButton value="sAVE" action="{!Savefun}"/>
<apex:commandButton value="cANCEL" action="{!cancel}" />
</apex:pageBlockButtons>
<apex:actionFunction name="priorityChangedJavaScript" action="{!priorityChanged}" rerender="out"/>
<apex:pageBlockSection title="Please Enter The Changes" columns="1" id="out" collapsible="false">
<apex:inputField value="{!poc.name}"/>
<apex:inputField value="{!poc.Hire_By__c}"/>
<apex:inputField value="{!poc.Hiring_Manager__c}"/>
<apex:inputField value="{!poc.Job_Level__c}"/>
<apex:inputField value="{!poc.Functional_Area__c}"/>
<apex:inputField value="{!poc.Job_Description__c}"/>
<apex:inputField value="{!poc.Location__c}"/>
<apex:inputField value="{!poc.Max_Pay__c}"/>
<apex:inputField value="{!poc.Min_Pay__c}"/>
<apex:inputField value="{!poc.Open_Date__c}"/>
<apex:inputField value="{!poc.Responsibilities__c}"/>
<apex:inputField value="{!poc.Travel_Required__c}"/>
<apex:inputField value="{!poc.Type__c}"/>
<apex:inputField value="{!poc.Status__c}" onchange="priorityChangedJavaScript()"/>
<apex:inputField value="{!poc.Cancellation_Reason__c}" rendered="{!show}"/>
<apex:outputPanel styleClass="myCustomMessage" rendered="{!showmessage}">
<apex:pageMessage severity="error" strength="1" summary="Please enter the reason" />
</apex:outputPanel>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Public class harishraoTeat{
Public static testMethod void harish1() {
Position__c b = new Position__c (Name='Behind the Cloud',
//add your fields data
);
insert b;
apexpages.currentpage().getparameters().put('id' , b.id );
ApexPages.StandardController sc = new ApexPages.StandardController(b);
actionFunctionController tr = new actionFunctionController (sc);
tr.Savefun()//addall methods likethis
}
}
test this modify some data any issue ask me . letme know working or not .
All Answers
Public class harishraoTeat{
Public static testMethod void harish1() {
Position__c b = new Position__c (Name='Behind the Cloud',
//add your fields data
);
insert b;
apexpages.currentpage().getparameters().put('id' , b.id );
ApexPages.StandardController sc = new ApexPages.StandardController(b);
actionFunctionController tr = new actionFunctionController (sc);
tr.Savefun()//addall methods likethis
}
}
test this modify some data any issue ask me . letme know working or not .
i had tryed with this code gettiong error:System.StringException: Invalid id: Rajendra Shukla
Hiring_Manager__c is a lookupfield
and shows the value from a user group i have created
/*Test Class For Controller Class : actionFunctionController
*
* Author: Rajendra Shukla
Created on: 9/15/2016
Last modified On:9/16/2016
What was Modified:
*/
@istest
public class Test_Action_contrller {
Static TestMethod void test_Method(){
Position__c po= new Position__c(name='Hari',Hire_By__c=Date.newInstance(2017,12,30),Hiring_Manager__c='Rajendra Shukla',Status__c='Cancelled',Cancellation_Reason__c='Cancelled',id=ApexPages.currentPage().getParameters().put('a012800000fNXyX'));
insert po;
apexpages.currentPage().getparameters().put('Id',po.Id);
ApexPages.StandardController sc= new ApexPages.StandardController(po);
actionFunctionController afc=new actionFunctionController(sc);
afc.priorityChanged();
afc.Savefun();
}
}