+ Start a Discussion
John NeilanJohn Neilan 

Test to Cover Page Redirect in VF Controller

Hello,
I have a custom VF controller and I'm trying to write some line in my test that will cover the Save and page redirect sections of the controller (lines 21-24 below).  Can anyone help me figure out the test code that will cover the page redirect?  Thanks,

Controller
public class VF_CQController{

public List<Client_Questionnaire__c> cq {get; set;}

    private final Account acct;
    public VF_CQController(ApexPages.StandardController myController){
        cq = new List<Client_Questionnaire__c>();
        acct=(Account)myController.getrecord();
    }

    public Client_Questionnaire__c cq2 = new Client_Questionnaire__c();
        public void clientQuest(){
       
            cq2.Account_Name__c = acct.id;
            cq.add(cq2);
        }
    
    public PageReference save() {
    insert cq;
        {
        PageReference RetPage = new PageReference('/apex/ClientQuestInitVerify?id=' + cq[0].id);
        RetPage.setRedirect(true);
        return RetPage; 
        }
    }
}

Test Class
@IsTest

public class TestVFControllers {

  static testMethod void Test_CQ_Overall() {
    
      User user1 = TestCreateRecords.createAMUser();
      insert user1;

      Account acct1 = TestCreateRecords.createAcct(0);
      insert acct1;
        
      Client_Questionnaire__c cq1 = TestCreateRecords.createCQNull(acct1.Id);

      ApexPages.StandardController sc1 = new ApexPages.standardController(acct1);

      VF_CQController cqCont1 = new VF_CQController(sc1);
          cqCont1.cq.add(cq1);

    Test.StartTest();
        cqCont1.clientQuest();
    Test.StopTest();
  }

 
KaranrajKaranraj
Hi John - call the save method in your test class
 
@IsTest

public class TestVFControllers {

  static testMethod void Test_CQ_Overall() {
    
      User user1 = TestCreateRecords.createAMUser();
      insert user1;

      Account acct1 = TestCreateRecords.createAcct(0);
      insert acct1;
        
      Client_Questionnaire__c cq1 = TestCreateRecords.createCQNull(acct1.Id);

      ApexPages.StandardController sc1 = new ApexPages.standardController(acct1);

      VF_CQController cqCont1 = new VF_CQController(sc1);
          cqCont1.cq.add(cq1);

    Test.StartTest();
        cqCont1.clientQuest();
       cqCont1.save();
    Test.StopTest();
  }



 
John NeilanJohn Neilan
Thanks Karanraj.  I tried that previously but the same lines are still not covered.  Any other suggestions?
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Try using your or Karanraj' code but with a slight modification in your controller (it will not affect its behavior):
 
public class VF_CQController{

public List<Client_Questionnaire__c> cq {get; set;}

    private final Account acct;
    public VF_CQController(ApexPages.StandardController myController){
        cq = new List<Client_Questionnaire__c>();
        acct=(Account)myController.getrecord();
    }

    public Client_Questionnaire__c cq2 = new Client_Questionnaire__c();
        public void clientQuest(){
       
            cq2.Account_Name__c = acct.id;
            cq.add(cq2);
        }
    
    public PageReference save() {
    insert cq;
        PageReference RetPage = new PageReference('/apex/ClientQuestInitVerify?id=' + cq[0].id);
        RetPage.setRedirect(true);
        return RetPage; 
    }
}

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
John NeilanJohn Neilan
Thanks.  However, the same lines are still not covered.