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
Ola BamideleOla Bamidele 

Writing Test Class

Hi Gurus, 

I have writen a very short apex code to be able redirect from one Visualforce page to another however, I now have to write a test class, which i am awful at :D 

So it would be great if someone could please assist me :)  

This is my short apex code for the redirection:
 
public with sharing class CustomerSatisfaction
 { public String currentRecordId {get;set;}

  public CustomerSatisfaction (ApexPages.StandardController controller)
    { this.currentRecordId = ApexPages.CurrentPage().getparameters().get('id');}
  
  public PageReference UK_Flag () {                   
           PageReference pageref = new 
                     PageReference('/apex/CustomerSatisfaction_EN?Id='+currentRecordId); 
                     pageref.setRedirect(true);
                     return pageref;}
 
      public PageReference France_Flag () {                   
           PageReference pageref = new 
                     PageReference('/apex/CustomerSatisfaction_FR?Id='+currentRecordId); 
                     pageref.setRedirect(true);
                     return pageref;}
 
}

If someone know how I could write the test then please let me know! Thanks!!
Best Answer chosen by Ola Bamidele
sfdcMonkey.comsfdcMonkey.com
hi ola, try following test class
@isTest
public class CustomerSatisfactionTest {

    public static testmethod void testOne(){
        account acc = new account();
          acc.Name = 'test';
        
          ApexPages.StandardController sc = new ApexPages.StandardController(acc);
            CustomerSatisfaction testAccPlan = new CustomerSatisfaction(sc);
              string str = testAccPlan.currentRecordId;  
               testAccPlan.UK_Flag();
               testAccPlan.France_Flag();
        
    }
}
Thanks let us know if it helps you
 

All Answers

sfdcMonkey.comsfdcMonkey.com
hi ola, try following test class
@isTest
public class CustomerSatisfactionTest {

    public static testmethod void testOne(){
        account acc = new account();
          acc.Name = 'test';
        
          ApexPages.StandardController sc = new ApexPages.StandardController(acc);
            CustomerSatisfaction testAccPlan = new CustomerSatisfaction(sc);
              string str = testAccPlan.currentRecordId;  
               testAccPlan.UK_Flag();
               testAccPlan.France_Flag();
        
    }
}
Thanks let us know if it helps you
 
This was selected as the best answer
Sampath SuranjiSampath Suranji
Hi,
Try below code.
@isTest
public class CustomerSatisfactionTest {
    
    public static  testmethod void UK_FlagTest(){
        //  ApexPages.StandardController con = new  ApexPages.StandardController();
        CustomerSatisfaction obj = new CustomerSatisfaction(null);
        PageReference pr = obj.UK_Flag();
        
        Account acc=new Account(name='test acc');//override this according to your object
        insert acc;
        ApexPages.CurrentPage().getparameters().put('id',acc.Id);
        ApexPages.StandardController sc = new ApexPages.StandardController(acc);
        CustomerSatisfaction objNew = new CustomerSatisfaction(sc); 
        PageReference prNew = objNew.UK_Flag();
    }
    public static  testmethod void France_FlagTest(){
        CustomerSatisfaction con = new CustomerSatisfaction(null);
        PageReference pr = con.France_Flag();
        
        Account acc=new Account(name='test acc');//override this according to your object
        insert acc;
        ApexPages.CurrentPage().getparameters().put('id',acc.Id);
        ApexPages.StandardController sc = new ApexPages.StandardController(acc);
        CustomerSatisfaction objNew = new CustomerSatisfaction(sc); 
        PageReference prNew = objNew.France_Flag();
    }
}

Note: you need to check null for currentRecordId value in your apex class's page reference methods. Otherwise you will get invalied redirect(page reference)

Best Regards
Sampath
Ola BamideleOla Bamidele
@ {!Piyush_soni__c}, 

lovely thanks!


@Sampath Suranji, the first reply worked so i didnt get a chance to try yours unfortunately , thanks though!