0

How do you make a test class for custom controller? My search results involves this but trying to apply it in my test class only create too many error that I don't understand.

public static testMethod void testMyController() {
    PageReference pageRef = Page.success;//Observe how we set the Page here
    Test.setCurrentPage(pageRef);

. Does my test class need those line of codes when my controller class is only very simple.

public class ClientInfo_VFcontroller{
    public Org_Info__c orgInfo {get; set;}
    public ClientInfo_VFcontroller(){
        orgInfo = new Org_Info__c();
    }
    public PageReference save(){
        insert orgInfo;
        orgInfo = null;
        ScheduleRetrieve.ScheduleRetrieve();
        return null;
    }
}
  • 2
    In this case your test does not need the call to setCurrentPage(). If you "pretend" like the class is just an Apex class, how would you test it? Can you just apply your normal testing logic/approach? – rael_kid Aug 27 '18 at 9:42
  • 1
    If you're running into errors, it generally helps to include the entire text of the error verbatim (i.e. without changing, adding, or removing any words). They might not make sense to you, but they'll probably make sense to more experienced eyes. – Derek F Aug 27 '18 at 13:03
2

How to cover pagereference method in test class for Custom Controller:-

  • Page reference to your VF Page:-

    PageReference pageRef = Page.TestPage; Test.setCurrentPage(pageRef);

  • Pass necessary parameter

    pageRef.getParameters().put('Id',id);

  • init controller

    CustomCtrl objCtrl = new CustomCtrl();

  • Call pageRef mymethod

    PageReference objPageRef = objCtrl.mymethod();

How to cover pagereference method in test class for Standard Controller:-

  • First create record

    Account acc = New Account(); acc.Name = 'Test Account'; INSERT acc;

  • Page reference to your VF Page

    PageReference pageRef = Page.TestPage; Test.setCurrentPage(pageRef);

  • Pass necessary parameter

    pageRef.getParameters().put('Id',acc.id);

  • Pass your object to controller

    ApexPages.StandardController stc = new ApexPages.StandardController(acc);

  • Call controller

    CustomCtrl objCtrl = new CustomCtrl(stc);

  • Call pageRef mymethod

    PageReference objPageRef = objCtrl.mymethod();

  • Put system asserts

    System.assertEquals (null,pageRef);

reference:- How to cover pagereference method in test class :-

Why use Test.setCurrentPage() method in a unit test?

In short once you set the context of the Page using method described, your controller is executing with parameters referred from the page context .

You basically indicate to your Unit test class the page you are testing your controller logic for. Your controller can be bound to multiple pages and setting page context for the unit test makes it crystal clear you are testing for specific page behaviour.

Reference:-Why use Test.setCurrentPage() method in a unit test?

So If you apex class is used as an extension of Visualforce page or custom controller build around ApexPages.StandardController, You can write inside your test class method as:-

PageReference pageRef = Page.success;
    Test.setCurrentPage(pageRef);
pageRef.getParameters().put('Id',Org_Info__c.id);
ClientInfo_VFcontroller objCtrl = new ClientInfo_VFcontroller();
objCtrl.save();

Update:- if ClientInfo_VFcontroller has no Id parameter and isn't a subclass or custom controller build around ApexPages.StandardController

    @isTest
public class testMyController {
    public static testmethod void TestQualityCheckController(){  

        ClientInfo_VFcontroller controllerOfFeedbackPage = new ClientInfo_VFcontroller();
        controllerOfFeedbackPage.save();
    }
}
  • 1
    ClientInfo_VFcontroller has no Id parameter and isn't a subclass or custom controller build around ApexPages.StandardController, so there's no need to work with that parameter. – David Reed Aug 27 '18 at 11:50
  • Yes, It's my bad @DavidReed I misinterpreted the question. I have updated my answer with both the scenario. – sanket kumar Aug 27 '18 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.