You need to sign in to do that
Don't have an account?
Pavan Kumar P
Test Class for Apex class with ApexPages.message
Hi,
Could you please help me in writing test class for below mentioned controller:
Visualforce Page:
<apex:page controller="ServiceTest">
<style type="text/css">
#title {
font-size: 150%;
margin-left: 30%;
}
</style>
<h2 id="title">Update Emp Records</h2><br/><br/>
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:inputField value="{!emps.Name}"/>
<apex:outputField value="{!emps.Name}"/>
<apex:inputField value="{!emps.Start_Date__c}"/>
<apex:outputField value="{!emps.Start_Date__c}"/>
<apex:inputField value="{!emps.End_Date__c}"/>
<apex:outputField value="{!emps.End_Date__c}"/>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton value="Update" action="{!UpdateRecord}"/>
</apex:pageBlockButtons>
<apex:pageMessage rendered="{!status}" severity="error" />
<apex:pageMessages />
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
public class ServiceTest {
public Employee__c emps {get;set;}
public boolean status {get;set;}
public ServiceTest ()
{
status= false;
emps = [select Name, Start_Date__c, End_Date__c from Employee__c LIMIT 1];
}
public PageReference updateRecord(){
if(emps.Start_Date__c > emps.End_Date__c){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Start Date should not be less than End Date'));
status= true;
}
else{
update emps;
}
return null;
}
}
Thanks in Advance.
Regards,
Pavan.
Could you please help me in writing test class for below mentioned controller:
Visualforce Page:
<apex:page controller="ServiceTest">
<style type="text/css">
#title {
font-size: 150%;
margin-left: 30%;
}
</style>
<h2 id="title">Update Emp Records</h2><br/><br/>
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:inputField value="{!emps.Name}"/>
<apex:outputField value="{!emps.Name}"/>
<apex:inputField value="{!emps.Start_Date__c}"/>
<apex:outputField value="{!emps.Start_Date__c}"/>
<apex:inputField value="{!emps.End_Date__c}"/>
<apex:outputField value="{!emps.End_Date__c}"/>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton value="Update" action="{!UpdateRecord}"/>
</apex:pageBlockButtons>
<apex:pageMessage rendered="{!status}" severity="error" />
<apex:pageMessages />
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
public class ServiceTest {
public Employee__c emps {get;set;}
public boolean status {get;set;}
public ServiceTest ()
{
status= false;
emps = [select Name, Start_Date__c, End_Date__c from Employee__c LIMIT 1];
}
public PageReference updateRecord(){
if(emps.Start_Date__c > emps.End_Date__c){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Start Date should not be less than End Date'));
status= true;
}
else{
update emps;
}
return null;
}
}
Thanks in Advance.
Regards,
Pavan.
Try using below test class for 100% coverage. I have implemented in two methods one for positive case to update and other for throwing error message scenario (Apex page Message Coverage).
Hope this helps! Please mark as best if it solves your issue.
Thanks
All Answers
Try using below test class for 100% coverage. I have implemented in two methods one for positive case to update and other for throwing error message scenario (Apex page Message Coverage).
Hope this helps! Please mark as best if it solves your issue.
Thanks
Its working !!! Thank you so much !!!!
Regards,
Pavan
It is working fine but the one of the test method is getting failed if there is any validation rule on Start_Date__c and End_Date__c fields.
Could you please help me in bypassing that error?
Thanks,
Pavan
Simple way is to set the values of Start date and end date in the test class such a way that validation rule doesn't trigger any errors (Not passing through criteria in rule). If thats' not possible you have to bypass validation rules for a given test class.
Please go through below link on how to bypass validation rule for test class:
http://theapexhub.com/bypass-validation-rules-while-running-apex-test-class/
Thanks