+ Start a Discussion
Pavan Kumar PPavan 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.
Best Answer chosen by Pavan Kumar P
SarvaniSarvani
Hi 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). 
@isTest public class ServiceTestClass{

 @isTest public static void testMethodToThrowError(){
     
     // Insert new Employee with valid start and end dates.
     Employee__c EM=new Employee__c();
     EM.Name='Test Employee';
     EM.Start_Date__c=Date.newInstance(2020, 12, 21);
     EM.End_Date__c=Date.newInstance(2021, 10, 1);
     Insert EM;
    // Fetch the Employee created and Update Start date greater than enddate to throw error.
     Employee__c fetched=[Select id,name from Employee__c where id=:EM.id ];
     fetched.Start_Date__c=Date.newInstance(2050, 10, 11);
     Update fetched;
    // Calling the Apex class method in which the Error logic is there
        Test.startTest();
         ServiceTest ST=new ServiceTest();
         ST.updateRecord();
         Test.stopTest();
 }
     @isTest public static void testMethodToUpdate(){
   // Insert new Employee with valid start and end dates. 
     Employee__c EM1=new Employee__c();
     EM1.Name='Test Employee 2';
     EM1.Start_Date__c=Date.newInstance(2020, 12, 21);
     EM1.End_Date__c=Date.newInstance(2021, 10, 1);
     Insert EM1;
 // Fetch the Employee created and Update Start date with valid date for Update.
     Employee__c fetched1=[Select id,name from Employee__c where id=:EM1.id ];
         fetched1.Start_Date__c=Date.newInstance(2018, 10, 11);
         Update fetched1;
         Test.startTest();
         ServiceTest ST=new ServiceTest();
         ST.updateRecord();
         Test.stopTest();
    
 }
 }

Hope this helps! Please mark as best if it solves your issue.

Thanks

All Answers

SarvaniSarvani
Hi 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). 
@isTest public class ServiceTestClass{

 @isTest public static void testMethodToThrowError(){
     
     // Insert new Employee with valid start and end dates.
     Employee__c EM=new Employee__c();
     EM.Name='Test Employee';
     EM.Start_Date__c=Date.newInstance(2020, 12, 21);
     EM.End_Date__c=Date.newInstance(2021, 10, 1);
     Insert EM;
    // Fetch the Employee created and Update Start date greater than enddate to throw error.
     Employee__c fetched=[Select id,name from Employee__c where id=:EM.id ];
     fetched.Start_Date__c=Date.newInstance(2050, 10, 11);
     Update fetched;
    // Calling the Apex class method in which the Error logic is there
        Test.startTest();
         ServiceTest ST=new ServiceTest();
         ST.updateRecord();
         Test.stopTest();
 }
     @isTest public static void testMethodToUpdate(){
   // Insert new Employee with valid start and end dates. 
     Employee__c EM1=new Employee__c();
     EM1.Name='Test Employee 2';
     EM1.Start_Date__c=Date.newInstance(2020, 12, 21);
     EM1.End_Date__c=Date.newInstance(2021, 10, 1);
     Insert EM1;
 // Fetch the Employee created and Update Start date with valid date for Update.
     Employee__c fetched1=[Select id,name from Employee__c where id=:EM1.id ];
         fetched1.Start_Date__c=Date.newInstance(2018, 10, 11);
         Update fetched1;
         Test.startTest();
         ServiceTest ST=new ServiceTest();
         ST.updateRecord();
         Test.stopTest();
    
 }
 }

Hope this helps! Please mark as best if it solves your issue.

Thanks
This was selected as the best answer
Pavan Kumar PPavan Kumar P
Hi Sarvani,

Its working !!! Thank you so much !!!!

Regards,
Pavan
Pavan Kumar PPavan Kumar P
Hi Sarvani,

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
 
SarvaniSarvani
Hi 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