function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mahanandeesh BelagantiMahanandeesh Belaganti 

hi all.i wrote a class which cancels the record in object and i have to testclass for it.

 hi all.i wrote a class which cancels the record in object and i have to write  testclass for it.as iam new to salesforce i cant able to write.i have seen in many websites but i didnt understand.please  help in this, if possible please write a test class for below code.so that i can understand it.

public class  IRTACancelController{

    public PageReference cancelData1() {
    pagereference pr=new pagereference('https://otap-developer-edition.ap1.force.com/employees/apex/IRTAMENU');
      pr.setRedirect(true);
      return pr;
   
    }


    public String getDisp() {
        return null;
    }

    public boolean displayPopUp { get; set; }
   
   public boolean valid1{set;get;}
   public boolean valid2{set;get;}
   public boolean valid3{set;get;}
   public boolean valid4{set;get;}
   public boolean valid5{set;get;}
   public boolean valid6{set;get;}
  
   public boolean msg1{set;get;}
   public boolean msg2{set;get;}
   public boolean msg3{set;get;}
   public boolean msg4{set;get;}
   public boolean msg5{set;get;}
   public boolean msg6{set;get;}
    public boolean mess{set;get;}
     public boolean mess1{set;get;}

     public boolean visible{set;get;}
     public boolean visiblemsg{set;get;}
  

    public attachment attach {get; set;}
    public String IRTAId { get; set; }
    public String accts {set;}
    public IRTA__c ir{ get; set;}
    public IRTA__c IRTArecord {get;set;}
    public IRTA__c istatus {get; set;}
    public user u{set;get;}
    public Boolean page1{get;set;}
   
    public user emp;
    public static Boolean forApproval1{get;set;}
     public IRTA__c irDetail{get;set;}
    public IRTACancelController(ApexPages.StandardController controller){
        page1 = false;
      
       
       
    }
   
    /*  public PageReference saveData1() {
          PageReference page1;
            //forApproval1 = false;
           
   //  User u = [select name from User where id =: UserInfo.getUserId()];
       update IRTArecord;
  
        system.debug('this is the message');
        ir.employee_number__c=Userinfo.getUserId();
           IRTArecord.Status__c= 'Inactive';

        update IRTArecord;
        update emp;
        //visible=false;
      
        //visiblemsg=false;  // update emp;
        // getUpload();
       
     page1  = new PageReference('https://otap-developer-edition.ap1.force.com/employees/apex/IRTADetail?irtaId='+irtarecord.id);
        

        page1.setRedirect(true);
        return page1;
       
       
        }*/
   
   
   
    public IRTACancelController(){
        page1 = false;
       
      ir = new IRTA__c();
       
       
       // showMessage = false;
       
         emp= [select first_name__c,Region__c,SUBSBU__c,Reporting_manager_id__c,Reporting_manger_Name__c,Sbu_Head_Id__c,Sbu_Head_Name__c,Rpo_Id__c,Rpo_Name__c,Corporate_MATC_Id__c,Corporate_MATC_Name__c,Regional_MATC_Id__c,Regional_MATC_Name__c,CFO_Id__c,CFO_Name__c,MD_ID__c,MD_Name__c,Central_Travel_Desk_Id__c,Central_Travel_Desk_Name__c,Last_Name__c,Surname__c,Sex__c,age__c ,Emergency_Contact_Number__c,Emergency_Contact_Name__c,Mediclaim_insurance_required__c,Grade__c,Accounting_location__c,Employee_Number__c,Office_Number__c,Extension_Number__c,SBU_Dept__c,Mobile_Number__c,Project_Id__c,Designation__c,Manager_id__c,Passport_number__c,Valid_Upto__c,Date_of_Issue__c,Issued_at__c,Residential_Address__c,Date_of_Birth__c,Place_of_Birth__c,Issued_By__c, Manager_Name__c,Project_Name__c from user where id=:UserInfo.getUserID() limit 1];
        
        String irId = ApexPages.currentPage().getParameters().get('irtaId');
    
         if(String.isNotBlank(irId))
         {
           irDetail = [select name, Period__c,Visa_Type__c,Visa_Required__c,  Duration__c,Accommodation__c,All_expenses__c from IRTA__C where id=:irId limit 1];
         }
 
  
    }
   
   
        public List<SelectOption> getAccts(){
        List<selectOption> options = new List<selectOption>();
        options.add(new selectOption('', '- None -'));
        for (IRTA__c ir: [SELECT Id, Name,status__c FROM IRTA__c where employee_number__c=:Userinfo.getUserId() and status__c='Active']) {
       
        options.add(new selectOption(ir.id, ir.Name));
       
        }
        return options;
     }

 


  
    public void displayData(){
     try{
               u=[SELECT name from user where id=:Userinfo.getUserId() limit 1];
               }
               catch (System.QueryException e) {
                  System.debug('caught ya!');
                }
     
        IRTArecord = [SELECT Period__c,Visa_Required__c,Visa_Type__c,Accommodation__c,All_expenses__c,Duration__c,Status__c from IRTA__C  where id =:IRTAId];
       
       
    
        //System.debug('Hello');
        //System.debug(record);
        page1 = true;
      // displayPopup=true;
      // Id  id = IRTAId;
       // PageReference IRTAInfo = new PageReference('/apex/IRTAInfo?id='+id);
       // return IRTAInfo;
        //return null;
  }
 
 
 
    public PageReference pagechange()
    {
  
        return null ;
  
     
         }
       
/* PageReference  
pagereference pr=new pagereference('https://otap-developer-edition.ap1.force.com/employees/apex/IRTAMENU');
      pr.setRedirect(true);
      return pr;
 */    
    
   


 
public pageReference home()
      {
      pagereference pr=new pagereference('https://otap-developer-edition.ap1.force.com/employees/apex/IRTAMENU');
      pr.setRedirect(true);
      return pr;
      }  
 
   
  public PageReference getupload()
    {
  
        return null ;
  
      }
        
        
        
    public void closePopup(){
          displayPopup=false;
      }

    public void showPopup() {
       displayPopup=true;
       
    }


Public PageReference sendEmailFunction(){
PageReference page1;
          system.debug('this is the message'+ir.employee_number__c);
          ir.employee_number__c=Userinfo.getUserId();
          IRTArecord.Status__c= 'Inactive';
          update IRTArecord;
          update emp;
          //String id1=ApexPages.currentPage().getParameters().get('id');
          //IRTA__c irt=[select Name from IRTA__c where id=:id1];
           

    try{
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
   String[] toAddresses = new String[] {'vignesh@gmail.com' (mailto:{'vignesh@gmail.com')};
  // String[] ccAddresses = new String[] {'mahanandeesh@gmail.com' (mailto:{'mahanandeesh@gmail.com')};
   mail.setToAddresses(toAddresses);
  // mail.setCcAddresses(ccAddresses);
   mail.setSenderDisplayName('Cancellation');
   mail.setSubject('IRTA Booking Cancel');
   System.debug('IRTA DEBUG'+IRTArecord.Name);
   mail.setPlainTextBody('User ' +u.name+' Canceled irta request '+IRTArecord.Name);
   //mail.setHtmlBody('<b> This is HTML body </b>' );
   Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
  }
  catch(Exception e){}
   page1=new pagereference('https://otap-developer-edition.ap1.force.com/employees/apex/IRTADetail?irtaId='+irtarecord.id);
   page1.setRedirect(true);
   return page1;
 }
}

Ankit AroraAnkit Arora
If you can wirte such a big class, then there is no difficulty writting a test class for it. I hope these links will help you more.

https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_test.htm

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_HelloWorld.htm